diff options
author | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2020-01-09 16:41:57 +0100 |
---|---|---|
committer | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2020-01-09 16:41:57 +0100 |
commit | 7891fda413ff314ff5cd977d96392988c16216e3 (patch) | |
tree | de7497fb57b567ac57c3f6f0451f09a219b69d14 /linked_list.c | |
download | csnake-7891fda413ff314ff5cd977d96392988c16216e3.tar.gz csnake-7891fda413ff314ff5cd977d96392988c16216e3.tar.bz2 csnake-7891fda413ff314ff5cd977d96392988c16216e3.zip |
initial commit
Diffstat (limited to 'linked_list.c')
-rw-r--r-- | linked_list.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/linked_list.c b/linked_list.c new file mode 100644 index 0000000..98b672b --- /dev/null +++ b/linked_list.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "linked_list.h" + +Node *initialise_node() +{ + Node* newNode = malloc(sizeof(Node)); + + newNode->next = NULL; + newNode->prev = NULL; + + return newNode; +} + +void free_node(Node* node) +{ + if(node) + free(node); + return; +} + +LinkedList *initialise_linked_list() +{ + LinkedList* newList = malloc(sizeof(LinkedList)); + + newList->head = NULL; + newList->tail = NULL; + + return newList; +} + +void free_linked_list(LinkedList* list) +{ + Node* element; + + while(list->head) + { + element = list->head->next; + free_node(list->head); + list->head = element; + } + free(list); +} + +void append_linked_list(LinkedList* list, int x, int y) +{ + Node* newNode = initialise_node(); + + newNode->x = x; + newNode->y = y; + + newNode->prev = list->tail; + + if(list->tail) + list->tail->next = newNode; + list->tail = newNode; + if(!list->head) + list->head = newNode; +} + +void remove_tail_linked_list(LinkedList* list) +{ + Node* element; + + if(!list->tail) + return; + element = list->tail->prev; + free_node(list->tail); + list->tail = element; + if(list->tail) + list->tail->next = NULL; + else + list->head = NULL; +} + |