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_insert(ll, a); // [ "one" ]
ds_sll_insert(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);

TODO

  • more data structures
  • tests
Description
some data structure implementations in c
Readme 52 KiB
Languages
C 99.1%
Makefile 0.9%