diff options
Diffstat (limited to 'linkedlist/list.c')
-rw-r--r-- | linkedlist/list.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/linkedlist/list.c b/linkedlist/list.c new file mode 100644 index 0000000..701cd5f --- /dev/null +++ b/linkedlist/list.c @@ -0,0 +1,46 @@ +#include "list.h" + +void list_init(struct list** _listroot) +{ + if(*_listroot) + while(list_popFront(_listroot)); + + *_listroot = NULL; +} + +void list_add(int _data, struct list** _listroot) +{ + struct list **current = _listroot; + + while(*current) + current = &(*current)->next; + + *current = malloc(sizeof(**current)); + (*current)->data = _data; +} + +int list_popFront(struct list** _listroot) +{ + if(!*_listroot) + return 0; + + int data = (*_listroot)->data; + struct list *oldRoot = *_listroot; + + *_listroot = (*_listroot)->next; + free(oldRoot); + + return data; +} + +int list_get(int _index, struct list** _listroot) +{ + struct list* current = *_listroot; + + for(int i = 0; i < _index; i++) + { + current = current->next; + } + + return current->data; +} |