From 43685c94ff9fe54c65bbd84340a0f5b185793d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Wed, 8 Jun 2016 23:38:35 +0300 Subject: [PATCH] Adding double linked circular list --- lab12/double.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lab12/double.c diff --git a/lab12/double.c b/lab12/double.c new file mode 100644 index 0000000..7e81a75 --- /dev/null +++ b/lab12/double.c @@ -0,0 +1,51 @@ +#include +#include + +typedef struct _dclist * List; + +struct _dclist { + int data; + List next; + List prev; +}; + +List init() { + List initializer = malloc(sizeof(struct _dclist)); + initializer->data = 0; + initializer->next = initializer; + initializer->prev = initializer; + return initializer; +} + +void insert(int data, List list) { + List head = list; + while ( list->next != head ) { // if we have next, go to it. + list = list->next; + } + list->next = malloc(sizeof(struct _dclist)); + head->prev = list->next; + list->next->prev = list; // set the preview value; + list->next->data = data; + list->next->next = head; +} + +void print(List list) { + List head = list; + while ( list->next != head) { + list = list->next; + printf("PREVIOUS VALUE: %d\n", list->prev->data); + printf("CURRENT VALUE: %d\n", list->data); + } + printf("Is it circular? %d means yes\n", list->next->data); +} + +int main(void) { + List test = init(); + insert(10, test); + insert(12, test); + insert(13, test); + insert(14, test); + insert(15, test); + print(test); + return 0; +}