From b390df531877c9a95cb53f3491e414e241880fe5 Mon Sep 17 00:00:00 2001 From: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> Date: Mon, 20 Sep 2021 17:10:59 +0530 Subject: [PATCH] chore(C): add doubly linked list (#456) --- algorithms/C/README.md | 1 + .../C/linked-lists/doubly-linked-list.c | 153 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 algorithms/C/linked-lists/doubly-linked-list.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 29958e52..b5c8a356 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -19,6 +19,7 @@ - [Circular Linked List](linked-lists/Insert-and-del-beginning-circular-ll.c) - [Merge two Linked Lists](linked-lists/merge-two-linkedlists.c) - [Reverse a Linked List](linked-lists/reverse-linkedlists.c) +- [Doubly Linked List](linked-lists/doubly-linked-list.c) ## Queues - [Double Ended Queue using array](queues/double-ended-queue-using-array.c) diff --git a/algorithms/C/linked-lists/doubly-linked-list.c b/algorithms/C/linked-lists/doubly-linked-list.c new file mode 100644 index 00000000..0a6abf04 --- /dev/null +++ b/algorithms/C/linked-lists/doubly-linked-list.c @@ -0,0 +1,153 @@ +/* +A doubly linked list is a linked data structure that consists of +a set of sequentially linked records called nodes. +Each node contains three fields: two link fields +(references to the previous and to the next node in the sequence of nodes) +and one data field. +*/ + +#include +#include + +#define HEAD(x) (x->head) +#define TAIL(x) (x->tail) +#define INC_NODE_COUNT(x) (x->count++) +#define DEC_NODE_COUNT(x) (x->count--) +#define SIZE(x) (x->count) + +typedef struct node_{ + int val; + struct node_* next; + struct node_* prev; +}node; + +typedef struct{ + unsigned int count; + node* head; + node* tail; +}dll; + +void init(dll* list){ + list->count = 0; + list->head = NULL; + list->tail = NULL; +} + +node* createnode(int val){ + node* n = (node*)malloc(sizeof(node)); + n->val = val; + return n; +} + +void push_front(dll* list, int val){ + node* new = createnode(val); + node* cur = HEAD(list); + INC_NODE_COUNT(list); + if(cur == NULL){ + HEAD(list) = new; + TAIL(list) = new; + return; + } + cur->prev = new; + new->next = cur; + list->head = new; +} + +void push_back(dll* list, int val){ + node* new = createnode(val); + node* cur = TAIL(list); + INC_NODE_COUNT(list); + if(cur == NULL){ + HEAD(list) = new; + TAIL(list) = new; + return; + } + cur->next = new; + new->prev = cur; + list->tail = new; +} + +int pop_front(dll* list){ + if(HEAD(list) == NULL){ + return -1; + } + node* cur = HEAD(list); + int val = cur->val; + HEAD(list) = HEAD(list)->next; + free(cur); + cur = NULL; + HEAD(list)->prev = NULL; + DEC_NODE_COUNT(list); + return val; +} + +int pop_back(dll* list){ + if(TAIL(list) == NULL){ + return -1; + } + node* cur = TAIL(list); + int val = cur->val; + TAIL(list) = TAIL(list)->prev; + free(cur); + cur = NULL; + TAIL(list)->next = NULL; + DEC_NODE_COUNT(list); + return val; +} + +void delete(dll* list){ + if(HEAD(list) == NULL){ + return; + } + node* cur = HEAD(list); + node* temp = NULL; + while(cur != NULL){ + temp = cur->next; + free(cur); + cur = NULL; + cur = temp; + } + SIZE(list) = 0; +} + +void print(dll* list){ + node* cur = list->head; + while(NULL != cur){ + printf("%i ", cur->val); + cur = cur->next; + } +} + +int main(){ + dll* list = (dll*)malloc(sizeof(dll)); + init(list); + + push_front(list, 3); + push_front(list, 2); + push_front(list, 1); + + push_back(list, 4); + push_back(list, 5); + push_back(list, 6); + + pop_front(list); + pop_back(list); + + print(list); + + delete(list); + + /* + + Time Complexity + --------------- + push_front: O(1) + push_back: O(1) + pop_front: O(1) + pop_back: O(1) + delete: O(N) + + */ + + return 0; +}