From 7891fda413ff314ff5cd977d96392988c16216e3 Mon Sep 17 00:00:00 2001 From: Manuel Palenzuela Date: Thu, 9 Jan 2020 16:41:57 +0100 Subject: initial commit --- linked_list.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 linked_list.c (limited to 'linked_list.c') 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 +#include + +#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; +} + -- cgit 1.4.1