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

14
ds.h
View File

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

1
test.c
View File

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