1.4 KiB
1.4 KiB
Data Structures
Probably not the most efficient, but I don't really care (it's still faster than the average site using js).
Singly/Doubly Linked List
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_ll_foreach(ds_sll_t, ll) {
puts(cur->data);
// one
// two
}
char *pa = ds_sll_remove(&ll, 0); // [ "two" ]
char *pb = ds_sll_remove(&ll, 0); // [ ]
If you need a doubly linked list just use the equivalent ds_dll functions and
datatypes.
Hash Map
typedef struct {
int num;
char *str;
} complex ;
complex *a = calloc(1, sizeof(complex));
a->num = 1;
a->str = "abc";
complex *b = calloc(1, sizeof(complex));
b->num = 2;
b->str = "def";
ds_hmp_t *hmp = ds_hmp_init(101);
ds_hmp_insert(hmp, a->str, a); // [ (23)[ [ a->str, a ] ] ]
ds_hmp_insert(hmp, b->str, b); // [ (23)[ [ a->str, a ] ], (58)[ [ b->str, b ] ]
complex *pa = ds_hmp_remove(hmp, a->str); // a
free(pa->str);
free(pa);
ds_hmp_free(&hmp, NULL);
free(b);
Stack
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
- tests