50 lines
1.1 KiB
Markdown
50 lines
1.1 KiB
Markdown
# 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):
|
|
```c
|
|
char *a = "one";
|
|
char *b = "two";
|
|
ds_sll_t *ll = ds_sll_new_node(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
|
|
```c
|
|
struct complex {
|
|
int num;
|
|
char *str;
|
|
};
|
|
|
|
struct complex *a = calloc(1, sizeof(struct complex));
|
|
a->num = 1;
|
|
a->str = "abc";
|
|
struct complex *b = calloc(1, sizeof(struct 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 ] ]
|
|
struct complex *pa = ds_hmp_remove(hmp, a->str); // a
|
|
free(pa->str);
|
|
free(pa);
|
|
ds_hmp_free(&hmp);
|
|
free(b);
|
|
```
|
|
|
|
# TODO
|
|
- [ ] more data structures
|
|
- [ ] tests
|