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.
This commit is contained in:
Squibid 2025-11-15 21:39:57 -05:00
parent 46b0219ab4
commit 69dc1dcb45
Signed by: squibid
GPG key ID: BECE5684D3C4005D
4 changed files with 104 additions and 16 deletions

View file

@ -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

66
ds.c
View file

@ -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;
}

32
ds.h
View file

@ -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

18
test.c
View file

@ -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",