summaryrefslogtreecommitdiff
path: root/src/ll.c
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-06-06 14:09:32 -0500
committer@syxhe <https://t.me/syxhe>2025-06-06 14:09:32 -0500
commit5431fec6726c18234c9c28b014cc6f18a0d79884 (patch)
treede2847fdc0858606b7d542c8470bb024ef27a42b /src/ll.c
parentec0cfdc492065dcce687797d3a931af105a461c8 (diff)
Style touchup
Diffstat (limited to 'src/ll.c')
-rw-r--r--src/ll.c43
1 files changed, 14 insertions, 29 deletions
diff --git a/src/ll.c b/src/ll.c
index b8d0d4c..908f25f 100644
--- a/src/ll.c
+++ b/src/ll.c
@@ -66,15 +66,12 @@ void dlinkedlist_free(void *dll) {
66} 66}
67 67
68int dlinkedlist_size(const dlinkedlist * const ll) { 68int dlinkedlist_size(const dlinkedlist * const ll) {
69 if(!ll) 69 if(!ll) ERRRET(EINVAL, -1);
70 RETURNWERR(EINVAL, -1);
71
72 return ll->size; 70 return ll->size;
73} 71}
74 72
75int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb fcb) { 73int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb fcb) {
76 if(!ll) 74 if(!ll) ERRRET(EINVAL, 1);
77 RETURNWERR(EINVAL, 1);
78 if(ll->size > 0) 75 if(ll->size > 0)
79 return 1; 76 return 1;
80 77
@@ -92,10 +89,7 @@ int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb f
92} 89}
93 90
94int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char op) { 91int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char op) {
95 if(!ll) 92 if(!ll || (op != 'a' && op != 'p')) ERRRET(EINVAL, 1);
96 RETURNWERR(EINVAL, 1);
97 if(op != 'a' && op != 'p')
98 RETURNWERR(EINVAL, 1);
99 93
100 int handleret; 94 int handleret;
101 if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0) 95 if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0)
@@ -126,6 +120,8 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char
126 ll->size++; 120 ll->size++;
127 return 0; 121 return 0;
128} 122}
123// TODO: Figure out where the memory leak gcc keeps complaining about is & fix it
124
129 125
130int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb) { 126int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb) {
131 return dlinkedlist_xxxend(ll, data, fcb, 'a'); 127 return dlinkedlist_xxxend(ll, data, fcb, 'a');
@@ -136,10 +132,8 @@ int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb) {
136} 132}
137 133
138dllnode * dlinkedlist_getnode(const dlinkedlist * const ll, int index) { 134dllnode * dlinkedlist_getnode(const dlinkedlist * const ll, int index) {
139 if(!ll) 135 if(!ll) ERRRET(EINVAL, NULL);
140 RETURNWERR(EINVAL, NULL); 136 if(index < 0 || index >= ll->size) ERRRET(EINVAL, NULL);
141 if(index < 0 || index >= ll->size)
142 RETURNWERR(EINVAL, NULL);
143 137
144 if(index == 0) 138 if(index == 0)
145 return ll->start; 139 return ll->start;
@@ -157,10 +151,8 @@ dllnode * dlinkedlist_getnode(const dlinkedlist * const ll, int index) {
157} 151}
158 152
159int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index) { 153int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index) {
160 if(!ll) 154 if(!ll) ERRRET(EINVAL, 1);
161 RETURNWERR(EINVAL, 1); 155 if(index < 0 || index >= ll->size) ERRRET(EINVAL, 1);
162 if(index < 0 || index >= ll->size)
163 RETURNWERR(EINVAL, 1);
164 156
165 // Handle the special cases of appending or prepending 157 // Handle the special cases of appending or prepending
166 if(index == 0) 158 if(index == 0)
@@ -188,10 +180,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i
188} 180}
189 181
190int dlinkedlist_remove(dlinkedlist * const ll, int index) { 182int dlinkedlist_remove(dlinkedlist * const ll, int index) {
191 if(!ll) 183 if(!ll) ERRRET(EINVAL, 1);
192 RETURNWERR(EINVAL, 1); 184 if(index < 0 || index >= ll->size) ERRRET(EINVAL, 2);
193 if(index < 0 || index >= ll->size)
194 RETURNWERR(EINVAL, 2);
195 185
196 dllnode *current = dlinkedlist_getnode(ll, index); 186 dllnode *current = dlinkedlist_getnode(ll, index);
197 if(!current) 187 if(!current)
@@ -226,10 +216,7 @@ void* dlinkedlist_get(const dlinkedlist * const ll, int index) {
226} 216}
227 217
228int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { 218int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) {
229 if(!ll) 219 if(!ll || callback == NULL) ERRRET(EINVAL, -1);
230 RETURNWERR(EINVAL, -1);
231 if(!callback)
232 RETURNWERR(EINVAL, -1);
233 220
234 for(dllnode *p = ll->start; p != NULL; p = p->next) 221 for(dllnode *p = ll->start; p != NULL; p = p->next)
235 callback(p->data); 222 callback(p->data);
@@ -238,10 +225,8 @@ int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) {
238} 225}
239 226
240void * dlinkedlist_poplast(dlinkedlist *ll) { 227void * dlinkedlist_poplast(dlinkedlist *ll) {
241 if(!ll) 228 if(!ll) ERRRET(EINVAL, NULL);
242 RETURNWERR(EINVAL, NULL); 229 if(dlinkedlist_isempty(ll)) ERRRET(ENODATA, NULL);
243 if(dlinkedlist_isempty(ll))
244 RETURNWERR(ENODATA, NULL);
245 230
246 void *data = dlinkedlist_get(ll, ll->size - 1); 231 void *data = dlinkedlist_get(ll, ll->size - 1);
247 dlinkedlist_remove(ll, ll->size - 1); 232 dlinkedlist_remove(ll, ll->size - 1);