From 69dc1dcb457089a94792fa255d992a70ccf487bb Mon Sep 17 00:00:00 2001 From: Squibid Date: Sat, 15 Nov 2025 21:39:57 -0500 Subject: [PATCH] feat(ll)!: add new ll function and rename insert ds_*ll_insert -> ds_*ll_append to better reflect what it does and there is now ds_*ll_add to add data anywhere in the ll. --- README.md | 4 ++-- ds.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- ds.h | 32 ++++++++++++++++++++++----- test.c | 18 ++++++++++----- 4 files changed, 104 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e8a3568..650a3c6 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ Example of working with a singly linked list (sll): char *a = "one"; char *b = "two"; ds_sll_t *ll = ds_sll_init(); -ds_sll_insert(ll, a); // [ "one" ] -ds_sll_insert(ll, b); // [ "one", "two" ] +ds_sll_append(ll, a); // [ "one" ] +ds_sll_append(ll, b); // [ "one", "two" ] ds_ll_foreach(ds_sll_t, ll) { puts(cur->data); // one diff --git a/ds.c b/ds.c index 661b482..25f36b3 100644 --- a/ds.c +++ b/ds.c @@ -40,7 +40,67 @@ ds_dll_t } int -ds_sll_insert(ds_sll_t *ll, void *data) +ds_sll_add(ds_sll_t **ll, void *data, unsigned x) +{ + int i; + ds_sll_t *cur, *prev, *new; + if (!ll || !data) { + return -1; + } + + for (i = 0, cur = *ll; cur; prev = cur, cur = cur->next, i++) { + if (i == x) { + if (!cur->data) { + cur->data = data; + return 0; + } + new = ds_sll_new_node(data); + new->next = cur; + if (i == 0) { + *ll = new; + } else { + prev->next = new; + } + return 0; + } + } + + return -1; +} + +int +ds_dll_add(ds_dll_t **ll, void *data, unsigned x) +{ + int i; + ds_dll_t *cur, *new; + if (!ll || !data) { + return -1; + } + + for (i = 0, cur = *ll; cur; cur = cur->next, i++) { + if (i == x) { + if (!cur->data) { + cur->data = data; + return 0; + } + new = ds_dll_new_node(data); + new->next = cur; + new->prev = cur->prev; + cur->prev = new; + if (i == 0) { + *ll = new; + } else { + cur->prev->next = new; + } + return 0; + } + } + + return -1; +} + +int +ds_sll_append(ds_sll_t *ll, void *data) { if (!ll || !data) { return -1; @@ -60,7 +120,7 @@ ds_sll_insert(ds_sll_t *ll, void *data) } int -ds_dll_insert(ds_dll_t *ll, void *data) +ds_dll_append(ds_dll_t *ll, void *data) { if (!ll || !data) { return -1; @@ -241,7 +301,7 @@ ds_hmp_insert(ds_hmp_t *hmp, char *key, void *data) /* get the ll and put the data into it */ ll = hmp->data[hash_pos]; - if (ds_sll_insert(ll, kv) != 0) { + if (ds_sll_append(ll, kv) != 0) { return -1; } diff --git a/ds.h b/ds.h index a8fbb3f..93e99db 100644 --- a/ds.h +++ b/ds.h @@ -55,24 +55,46 @@ ds_sll_t *ds_sll_new_node(void *data); ds_dll_t *ds_dll_new_node(void *data); /** - * @brief data to insert into a singly linked list. This will use + * @brief data to add at x of a singly linked list. This will use * ds_sll_new_node to create the node for you. * * @param ll singly linked list * @param data data you want to add + * @param x the position to add it at * @return 0 on success */ -int ds_sll_insert(ds_sll_t *ll, void *data); +int ds_sll_add(ds_sll_t **ll, void *data, unsigned x); /** - * @brief data to insert into a doubly linked list. This will use - * ds_sll_new_node to create the node for you. + * @brief data to add at x of a doubly linked list. This will use + * ds_dll_new_node to create the node for you. * * @param ll doubly linked list * @param data data you want to add + * @param x the position to add it at * @return 0 on success */ -int ds_dll_insert(ds_dll_t *ll, void *data); +int ds_dll_add(ds_dll_t **ll, void *data, unsigned x); + +/** + * @brief data to append to the end of a singly linked list. This will use + * ds_sll_new_node to create the node for you. + * + * @param ll singly linked list + * @param data data you want to append + * @return 0 on success + */ +int ds_sll_append(ds_sll_t *ll, void *data); + +/** + * @brief data to append to the end of a doubly linked list. This will use + * ds_dll_new_node to create the node for you. + * + * @param ll doubly linked list + * @param data data you want to append + * @return 0 on success + */ +int ds_dll_append(ds_dll_t *ll, void *data); /** * @brief remove an index from a singly linked list diff --git a/test.c b/test.c index 2e99a9a..dc84bc9 100644 --- a/test.c +++ b/test.c @@ -30,29 +30,35 @@ int main(int argc, char *argv[]) { test("sll", - char *a = "one"; + char *a = "a"; + char *b = "b"; ds_sll_t *tmp; it("creates a new linked list", tmp = ds_sll_init()); free(tmp); ds_sll_t *ll = ds_sll_new_node(a); - it("inserts an invalid item", ds_sll_insert(ll, NULL) == -1); + it("appends an invalid item", ds_sll_append(ll, NULL) == -1); it("removes an invalid index", ds_sll_remove(&ll, 1234) == NULL); - it("removes an item", a == ds_sll_remove(&ll, 0)); + it("adds an item to the beginning", ds_sll_add(&ll, b, 0) == 0); + it("removes an item", b == ds_sll_remove(&ll, 0)); + ds_sll_remove(&ll, 0); ); test("dll", - char *a = "one"; + char *a = "a"; + char *b = "b"; ds_dll_t *tmp; it("creates a new linked list", tmp = ds_dll_init()); free(tmp); ds_dll_t *ll = ds_dll_new_node(a); - it("inserts an invalid item", ds_dll_insert(ll, NULL) == -1); + it("appends an invalid item", ds_dll_append(ll, NULL) == -1); it("removes an invalid index", ds_dll_remove(&ll, 1234) == NULL); - it("removes an item", a == ds_dll_remove(&ll, 0)); + it("adds an item to the beginning", ds_dll_add(&ll, b, 0) == 0); + it("removes an item", b == ds_dll_remove(&ll, 0)); + ds_dll_remove(&ll, 0); ); test("hmp",