Compare commits

..

No commits in common. "9daffd4ed1883ea50ae94b14275b46937cc348ce" and "5e4ae41113e63004bd131010853e5265b31302d3" have entirely different histories.

5 changed files with 241 additions and 459 deletions

2
.gitignore vendored
View file

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

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_append(ll, a); // [ "one" ]
ds_sll_append(ll, b); // [ "one", "two" ]
ds_sll_insert(ll, a); // [ "one" ]
ds_sll_insert(ll, b); // [ "one", "two" ]
ds_ll_foreach(ds_sll_t, ll) {
puts(cur->data);
// one
@ -45,20 +45,6 @@ ds_hmp_free(&hmp, NULL);
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
- [ ] more data structures
- [x] tests

119
ds.c
View file

@ -40,67 +40,7 @@ ds_dll_t
}
int
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)
ds_sll_insert(ds_sll_t *ll, void *data)
{
if (!ll || !data) {
return -1;
@ -120,7 +60,7 @@ ds_sll_append(ds_sll_t *ll, void *data)
}
int
ds_dll_append(ds_dll_t *ll, void *data)
ds_dll_insert(ds_dll_t *ll, void *data)
{
if (!ll || !data) {
return -1;
@ -301,7 +241,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_append(ll, kv) != 0) {
if (ds_sll_insert(ll, kv) != 0) {
return -1;
}
@ -367,56 +307,3 @@ void
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,11 +22,6 @@ typedef struct {
void *val;
} _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)
/**
@ -60,46 +55,24 @@ ds_sll_t *ds_sll_new_node(void *data);
ds_dll_t *ds_dll_new_node(void *data);
/**
* @brief data to add at x of a singly linked list. This will use
* @brief data to insert into 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_add(ds_sll_t **ll, void *data, unsigned x);
int ds_sll_insert(ds_sll_t *ll, void *data);
/**
* @brief data to add at x of a doubly linked list. This will use
* ds_dll_new_node to create the node for you.
* @brief data to insert into a doubly linked list. This will use
* ds_sll_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_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);
int ds_dll_insert(ds_dll_t *ll, void *data);
/**
* @brief remove an index from a singly linked list
@ -173,37 +146,4 @@ void *ds_hmp_get(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

49
test.c
View file

@ -1,4 +1,3 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -12,13 +11,12 @@
tests \
_tests_passed += _tests; \
printf("%s: %d/%d tests passed\n", name, _tests_passed, _tests); \
} while (0)
} while (0)
#define it(message, test) do { \
_tests++; \
if (!(test)) { \
printf("%s %s FAILED @ %s:%d\n", _test_name, message, __FILE__, __LINE__); \
printf(" -> test condition: (%s)\n", #test); \
printf("%s %s FAILED\n", _test_name, message); \
_tests_passed--; \
} \
} while (0)
@ -32,35 +30,29 @@ int
main(int argc, char *argv[])
{
test("sll",
char *a = "a";
char *b = "b";
char *a = "one";
ds_sll_t *tmp;
it("creates a new linked list", tmp = ds_sll_init());
it("ensures creates a new linked list", tmp = ds_sll_init());
free(tmp);
ds_sll_t *ll = ds_sll_new_node(a);
it("appends an invalid item", ds_sll_append(ll, NULL) == -1);
it("inserts an invalid item", ds_sll_insert(ll, NULL) == -1);
it("removes an invalid index", ds_sll_remove(&ll, 1234) == NULL);
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);
it("removes an item", a == ds_sll_remove(&ll, 0));
);
test("dll",
char *a = "a";
char *b = "b";
char *a = "one";
ds_dll_t *tmp;
it("creates a new linked list", tmp = ds_dll_init());
it("ensures creates a new linked list", tmp = ds_dll_init());
free(tmp);
ds_dll_t *ll = ds_dll_new_node(a);
it("appends an invalid item", ds_dll_append(ll, NULL) == -1);
it("inserts an invalid item", ds_dll_insert(ll, NULL) == -1);
it("removes an invalid index", ds_dll_remove(&ll, 1234) == NULL);
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);
it("removes an item", a == ds_dll_remove(&ll, 0));
);
test("hmp",
@ -78,25 +70,4 @@ main(int argc, char *argv[])
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);
);
}