diff --git a/C-RK/C-RK-Week2/llist.c b/C-RK/C-RK-Week2/llist.c index 92dffbe..d38f003 100644 --- a/C-RK/C-RK-Week2/llist.c +++ b/C-RK/C-RK-Week2/llist.c @@ -20,20 +20,23 @@ int add(int data) if(NULL == pn) { - printf("Out of memory ..."); + printf("Out of memory ...\n"); } else { if(NULL == pHead) { pn->data = data; + pn->previous = NULL; pn->next = NULL; pHead = pn; } else { + pHead->next = pn; pn->data = data; - pn->next = pHead; + pn->previous = pHead; + pn->next = NULL; pHead = pn; } } @@ -46,10 +49,34 @@ void show() struct node *p = pHead; int nr = 0; - for( ; NULL != p->next ; p = p->next ) + if (NULL == pHead) { - printf("node nr: %d heeft data [%d]\n",nr++,p->data); + printf("De lijst is leeg\n"); + } + else + { + for (; NULL != p->previous; p = p->previous) + { + printf("node nr: %d heeft data [%d]\n", nr++, p->data); + } } } +void clear() +{ + struct node *p = pHead; + struct node *prev = NULL; + + for (; NULL != p->previous;) + { + prev = p->previous; + free(p); + p = prev; + } + + pHead = NULL; + + printf("De gehele lijst in gecleared!\n"); +} + diff --git a/C-RK/C-RK-Week2/llist.h b/C-RK/C-RK-Week2/llist.h index c65d6a8..7d4f87c 100644 --- a/C-RK/C-RK-Week2/llist.h +++ b/C-RK/C-RK-Week2/llist.h @@ -5,6 +5,7 @@ struct node { int data; struct node *next; + struct node *previous; }; void init(); diff --git a/C-RK/C-RK-Week2/main.c b/C-RK/C-RK-Week2/main.c index 3c8d611..f5b504e 100644 --- a/C-RK/C-RK-Week2/main.c +++ b/C-RK/C-RK-Week2/main.c @@ -5,13 +5,27 @@ int main() { int idx; + printf("Running init...\n"); + init(); + printf("Adding data...\n"); + for(idx = 0; idx < 10; idx++) { add(idx); } + + printf("Showing data...\n"); show(); + printf("Clearing data...\n"); + clear(); + + printf("Showing data...\n"); + show(); + + printf("Done\n"); + return 1; }