Compare commits

...

7 commits

Author SHA1 Message Date
9daffd4ed1
fix: minor formatting 2025-11-15 21:49:00 -05:00
edbe9ba3b8
fix(test): improve testing macros 2025-11-15 21:48:41 -05:00
945343daa0
feat(stack): introduce a stack ds, internally this uses a sll 2025-11-15 21:47:45 -05:00
69dc1dcb45
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.
2025-11-15 21:43:29 -05:00
46b0219ab4
fix(gitignore): add some more artifacts to ignore 2025-11-15 02:20:39 -05:00
a56daaf6b8
fix(formatting): :retab 2025-11-15 02:19:58 -05:00
5a485445ed
fix(test): remove extra word 2025-11-15 02:19:31 -05:00
5 changed files with 459 additions and 241 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
test test
.cache
compile_commands.json

View file

@ -8,8 +8,8 @@ Example of working with a singly linked list (sll):
char *a = "one"; char *a = "one";
char *b = "two"; char *b = "two";
ds_sll_t *ll = ds_sll_init(); ds_sll_t *ll = ds_sll_init();
ds_sll_insert(ll, a); // [ "one" ] ds_sll_append(ll, a); // [ "one" ]
ds_sll_insert(ll, b); // [ "one", "two" ] ds_sll_append(ll, b); // [ "one", "two" ]
ds_ll_foreach(ds_sll_t, ll) { ds_ll_foreach(ds_sll_t, ll) {
puts(cur->data); puts(cur->data);
// one // one
@ -45,6 +45,20 @@ ds_hmp_free(&hmp, NULL);
free(b); free(b);
``` ```
## Stack
```c
char *a = "a";
char *b = "b";
ds_stack_t *stack = ds_stack_init();
ds_stack_push(stack, a); // [ "a" ]
ds_stack_push(stack, b); // [ "b", "a" ]
ds_stack_peek(stack, 0); // "b"
ds_stack_pop(stack); // "b"
ds_stack_pop(stack); // "a"
free(stack);
```
# TODO # TODO
- [ ] more data structures - [ ] more data structures
- [x] tests - [x] tests

119
ds.c
View file

@ -40,7 +40,67 @@ ds_dll_t
} }
int 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) { if (!ll || !data) {
return -1; return -1;
@ -60,7 +120,7 @@ ds_sll_insert(ds_sll_t *ll, void *data)
} }
int int
ds_dll_insert(ds_dll_t *ll, void *data) ds_dll_append(ds_dll_t *ll, void *data)
{ {
if (!ll || !data) { if (!ll || !data) {
return -1; 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 */ /* get the ll and put the data into it */
ll = hmp->data[hash_pos]; ll = hmp->data[hash_pos];
if (ds_sll_insert(ll, kv) != 0) { if (ds_sll_append(ll, kv) != 0) {
return -1; return -1;
} }
@ -307,3 +367,56 @@ void
return NULL; return NULL;
} }
ds_stack_t
*ds_stack_init(void)
{
ds_stack_t *stack = malloc(sizeof(ds_stack_t));
stack->n = 0;
return stack;
}
int
ds_stack_push(ds_stack_t *stack, void *data)
{
if (stack->n == 0) {
stack->items = ds_sll_new_node(data);
stack->n++;
return 0;
}
int r = ds_sll_add(&stack->items, data, 0);
if (!r) {
stack->n++;
}
return r;
}
void
*ds_stack_pop(ds_stack_t *stack)
{
void *r = ds_sll_remove(&stack->items, 0);
if (r != NULL) {
stack->n--;
}
return r;
}
void
*ds_stack_peek(ds_stack_t *stack, unsigned x)
{
int i;
ds_sll_t *cur;
if (x > stack->n) {
return NULL;
}
for (cur = stack->items, i = 0; cur; cur = cur->next, i++) {
if (i == x) {
return cur->data;
}
}
return NULL;
}

70
ds.h
View file

@ -22,6 +22,11 @@ typedef struct {
void *val; void *val;
} _ds_hmp_kv_t; } _ds_hmp_kv_t;
typedef struct {
unsigned n;
ds_sll_t *items;
} ds_stack_t;
#define ds_ll_foreach(t, ll) for (t *cur = ll; cur; cur = cur->next) #define ds_ll_foreach(t, ll) for (t *cur = ll; cur; cur = cur->next)
/** /**
@ -55,24 +60,46 @@ ds_sll_t *ds_sll_new_node(void *data);
ds_dll_t *ds_dll_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. * ds_sll_new_node to create the node for you.
* *
* @param ll singly linked list * @param ll singly linked list
* @param data data you want to add * @param data data you want to add
* @param x the position to add it at
* @return 0 on success * @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 * @brief data to add at x of a doubly linked list. This will use
* ds_sll_new_node to create the node for you. * ds_dll_new_node to create the node for you.
* *
* @param ll doubly linked list * @param ll doubly linked list
* @param data data you want to add * @param data data you want to add
* @param x the position to add it at
* @return 0 on success * @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 * @brief remove an index from a singly linked list
@ -146,4 +173,37 @@ void *ds_hmp_get(ds_hmp_t *hmp, char *key);
*/ */
void *ds_hmp_remove(ds_hmp_t *hmp, char *key); void *ds_hmp_remove(ds_hmp_t *hmp, char *key);
/**
* @brief initialize a new stack
*
* @return the stack
*/
ds_stack_t *ds_stack_init(void);
/**
* @brief push new data onto the stack
*
* @param stack the stack
* @param data the data
* @return 0 on success
*/
int ds_stack_push(ds_stack_t *stack, void *data);
/**
* @brief pop the data off the top of the stack
*
* @param stack the stack
* @return the data
*/
void *ds_stack_pop(ds_stack_t *stack);
/**
* @brief peek forward x items
*
* @param stack the stack
* @param x how many items to look ahead
* @return the data or NULL if out of range
*/
void *ds_stack_peek(ds_stack_t *stack, unsigned x);
#endif #endif

47
test.c
View file

@ -1,3 +1,4 @@
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -16,7 +17,8 @@
#define it(message, test) do { \ #define it(message, test) do { \
_tests++; \ _tests++; \
if (!(test)) { \ if (!(test)) { \
printf("%s %s FAILED\n", _test_name, message); \ printf("%s %s FAILED @ %s:%d\n", _test_name, message, __FILE__, __LINE__); \
printf(" -> test condition: (%s)\n", #test); \
_tests_passed--; \ _tests_passed--; \
} \ } \
} while (0) } while (0)
@ -30,29 +32,35 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
test("sll", test("sll",
char *a = "one"; char *a = "a";
char *b = "b";
ds_sll_t *tmp; ds_sll_t *tmp;
it("ensures creates a new linked list", tmp = ds_sll_init()); it("creates a new linked list", tmp = ds_sll_init());
free(tmp); free(tmp);
ds_sll_t *ll = ds_sll_new_node(a); 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 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", test("dll",
char *a = "one"; char *a = "a";
char *b = "b";
ds_dll_t *tmp; ds_dll_t *tmp;
it("ensures creates a new linked list", tmp = ds_dll_init()); it("creates a new linked list", tmp = ds_dll_init());
free(tmp); free(tmp);
ds_dll_t *ll = ds_dll_new_node(a); 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 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", test("hmp",
@ -70,4 +78,25 @@ main(int argc, char *argv[])
free(a); free(a);
); );
test("stack",
char *a = "a";
char *b = "b";
char *c = "c";
ds_stack_t *stack = ds_stack_init();
it("pushes an item", ds_stack_push(stack, a) == 0);
it("pops an item", ds_stack_pop(stack) == a);
ds_stack_push(stack, b);
ds_stack_push(stack, c);
it("peeks ahead", ds_stack_peek(stack, 0) == c);
it("peeks past the stack", ds_stack_peek(stack, INT_MAX) == NULL);
ds_stack_pop(stack);
ds_stack_pop(stack);
free(stack);
);
} }