From ca21434c9cd4ab7d3f736be357fc77b4ef814488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Fri, 20 May 2016 15:26:24 +0300 Subject: [PATCH] Finishing first problem --- lab12/1.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/lab12/1.c b/lab12/1.c index 0d27fed..e8d4c5f 100644 --- a/lab12/1.c +++ b/lab12/1.c @@ -1 +1,69 @@ -// to do +#include +#include + +/* Double Linked list related code */ + +typedef struct _dilist * Dlist; + +struct _dilist { + int element; + Dlist next; + Dlist prev; +}; + +Dlist insert(int element, Dlist list) { + Dlist * addr = &list; + Dlist prev = NULL; + while (*addr) { // While addr' not null, go to the end. + if ( !(*addr)->next ) { + prev = *addr; + } + addr = &(*addr)->next; // the list is modified by this. I think... + } // we should be at the end now. + Dlist node = malloc(sizeof(struct _dilist)); + node->element = element; + node->next = NULL; // we set to head if we want circular, I think? + node->prev = prev; // 1 - NULL, 2 - 1, 3- 2, 4 - 3 + // printf("%d", prev->element); + *addr = node; + return list; +} + +void print_list(Dlist list) { + printf("DLIST PRINT\n"); + Dlist * addr = &list; + while (*addr) { + // if (((*addr)->prev)) + // printf("prev %d\n", ((*addr)->prev)->element); + printf("%d ", (*addr)->element); + addr = &(*addr)->next; + } + printf("\n"); +} + +Dlist reverse(Dlist list) { + short virgin = 1; + // creeaza spatiu pentru lista aia noua + Dlist reversed = malloc(sizeof(struct _dilist)); // shoul find better names + // sa duce la ultimu element + while(list->next) { list = list->next; } + while(list) { + if (virgin) { + reversed = insert(list->element, NULL); + virgin = 0; + } else { + reversed = insert(list->element, reversed); + } + list = list->prev; + } + return reversed; +} + +int main(void) { + Dlist dli = insert(1, insert(2, insert(3, insert(4, NULL)))); + Dlist dli2 = reverse(dli); + print_list(dli); + print_list(dli2); + print_list(dli); + return 0; +}