diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-03 23:35:27 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-02-03 23:35:27 +0100 |
commit | 82c63a1f2422c3f4b1599bcb889e3ed1fc4f6ed0 (patch) | |
tree | c433dbc1621492794c6d9993f4939aa68c20940b /linked_list.inc.h | |
parent | Loads of memmory fixes, among other. (diff) | |
download | calp-82c63a1f2422c3f4b1599bcb889e3ed1fc4f6ed0.tar.gz calp-82c63a1f2422c3f4b1599bcb889e3ed1fc4f6ed0.tar.xz |
Add linked list generic type.
Diffstat (limited to '')
-rw-r--r-- | linked_list.inc.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/linked_list.inc.h b/linked_list.inc.h new file mode 100644 index 00000000..260eaf0d --- /dev/null +++ b/linked_list.inc.h @@ -0,0 +1,61 @@ +#ifndef TYPE +#error "Set TYPE before including this file" +#else + +int CONSTRUCTOR_DECL ( LLIST(TYPE) ) { + this->length = 0; + this->head = NULL; + this->tail = NULL; + this->cur = NULL; + return 0; +} + +int LLIST_FREE(TYPE) (LLIST(TYPE)* this ) { + LINK(TYPE) *node, *next; + node = this->head; + while (node != NULL) { + next = node->after; + FFREE(TYPE, node->value); + free(node); + node = next; + } + this->length = 0; + return 0; +} + +int CONSTRUCTOR_DECL ( LINK(TYPE) ) { + this->before = NULL; + this->after = NULL; + this->value = NULL; + return 0; +} + +int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val) { + NEW(LINK(TYPE), l); + l->after = lst->head; + if (l->after != NULL) { + l->after->before = l; + } + lst->head = l; + l->value = val; + ++lst->length; + + lst->cur = l; + + return 0; +} + +int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) { + LINK(TYPE)* n = src->head; + + while (n != NULL) { + NEW(TYPE, cpy); + DEEP_COPY(TYPE)(cpy, n->value); + LLIST_CONS(TYPE) ( dest, cpy ); + n = n->after; + } + + return 0; +} + +#endif /* TYPE */ |