Compare commits

...

2 Commits

Author SHA1 Message Date
16470f2592 don't crash when removing out of bounds 2025-08-16 23:38:14 -04:00
db42c21754 try and be a bit more standard 2025-08-16 23:37:56 -04:00
3 changed files with 17 additions and 8 deletions

10
ds.c
View File

@ -67,7 +67,7 @@ void
return NULL; return NULL;
} }
for (i = -1, cur = *ll;; i++, cur = cur->next) { for (i = -1, cur = *ll; cur; i++, cur = cur->next) {
if (i + 1 == idx) { if (i + 1 == idx) {
if (idx == 0) { if (idx == 0) {
rm = cur; rm = cur;
@ -86,6 +86,10 @@ void
} }
} }
if (!rm) {
return NULL;
}
data = rm->data; data = rm->data;
free(rm); free(rm);
return data; return data;
@ -127,6 +131,10 @@ void
} }
} }
if (!rm) {
return NULL;
}
data = rm->data; data = rm->data;
free(rm); free(rm);
return data; return data;

14
ds.h
View File

@ -1,14 +1,14 @@
#ifndef _DS_LOADED #ifndef DS_H
#define _DS_LOADED #define DS_H
typedef struct _sll { typedef struct _ds_sll {
struct _sll *next; struct _ds_sll *next;
void *data; void *data;
} ds_sll_t; } ds_sll_t;
typedef struct _dll { typedef struct _ds_dll {
struct _dll *next; struct _ds_dll *next;
struct _dll *prev; struct _ds_dll *prev;
void *data; void *data;
} ds_dll_t; } ds_dll_t;

1
test.c
View File

@ -17,6 +17,7 @@ main(int argc, char *argv[])
ds_ll_foreach(ds_sll_t, ll) { ds_ll_foreach(ds_sll_t, ll) {
puts(cur->data); puts(cur->data);
} }
ds_sll_remove(&ll, 1234);
char *pa = ds_sll_remove(&ll, 0); // [ "two" ] char *pa = ds_sll_remove(&ll, 0); // [ "two" ]
char *pb = ds_sll_remove(&ll, 0); // [ ] char *pb = ds_sll_remove(&ll, 0); // [ ]