From b8591ad2eb95ceb18c9e7d6af7a4688add4bdce5 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Wed, 17 Feb 2016 12:11:54 +0100 Subject: [PATCH] Add at position --- C-RK/C-RK-Week2/llist.c | 88 ++++++++++++++++++++++++++++++++++------- C-RK/C-RK-Week2/llist.h | 4 +- C-RK/C-RK-Week2/main.c | 24 ++++++++++- 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/C-RK/C-RK-Week2/llist.c b/C-RK/C-RK-Week2/llist.c index d38f003..db40e69 100644 --- a/C-RK/C-RK-Week2/llist.c +++ b/C-RK/C-RK-Week2/llist.c @@ -3,18 +3,19 @@ #include "llist.h" -static struct node *pHead = NULL; +static struct node *pEnd = NULL; +static struct node *pStart = NULL; void init() { - pHead = NULL; - + pEnd = NULL; + pStart = NULL; } /* * Aan de voorkant invoegen */ -int add(int data) +int add(int data, int begin) { struct node *pn = (struct node*)malloc(sizeof(struct node)); @@ -24,38 +25,96 @@ int add(int data) } else { - if(NULL == pHead) + if (NULL == pStart) { pn->data = data; pn->previous = NULL; pn->next = NULL; - pHead = pn; + pStart = pn; + pEnd = pn; + } + else if (begin) + { + pStart->previous = pn; + pn->data = data; + pn->next = pStart; + pn->previous = NULL; + pStart = pn; } else { - pHead->next = pn; + pEnd->next = pn; pn->data = data; - pn->previous = pHead; + pn->previous = pEnd; pn->next = NULL; - pHead = pn; + pEnd = pn; } } return 1; } +int addPos(int data, int positie) +{ + struct node *pn = (struct node*)malloc(sizeof(struct node)); + + if (NULL == pn) + { + printf("Out of memory ...\n"); + } + else + { + if (NULL == pStart) + { + add(data, '1'); + } + else + { + if (positie < 0) + { + printf("Positie < 0 : %d ...\n", positie); + return 0; + } + + int idx = 0; + struct node *p = pStart; + + for (; NULL != p->next; p = p->next) + { + if (positie == idx) + { + pn->data = data; + pn->next = p; + pn->previous = p->previous; + p->previous->next = pn; + p->previous = pn; + return 1; + } + + idx++; + } + + printf("Positie > %d : %d ...\n", idx, positie); + free(pn); + return 0; + } + } + + return 1; +} + void show() { - struct node *p = pHead; + struct node *p = pStart; int nr = 0; - if (NULL == pHead) + if (NULL == pStart) { printf("De lijst is leeg\n"); } else { - for (; NULL != p->previous; p = p->previous) + for (; NULL != p->next; p = p->next) { printf("node nr: %d heeft data [%d]\n", nr++, p->data); } @@ -64,7 +123,7 @@ void show() void clear() { - struct node *p = pHead; + struct node *p = pEnd; struct node *prev = NULL; for (; NULL != p->previous;) @@ -74,7 +133,8 @@ void clear() p = prev; } - pHead = NULL; + pEnd = NULL; + pStart = 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 7d4f87c..0047d20 100644 --- a/C-RK/C-RK-Week2/llist.h +++ b/C-RK/C-RK-Week2/llist.h @@ -9,7 +9,9 @@ struct node }; void init(); -int add(int data); +int add(int data, int begin); +int addPos(int data, int positie); void show(); +void clear(); #endif diff --git a/C-RK/C-RK-Week2/main.c b/C-RK/C-RK-Week2/main.c index f5b504e..8f01315 100644 --- a/C-RK/C-RK-Week2/main.c +++ b/C-RK/C-RK-Week2/main.c @@ -9,11 +9,11 @@ int main() init(); - printf("Adding data...\n"); + printf("Adding data at end...\n"); for(idx = 0; idx < 10; idx++) { - add(idx); + add(idx, '0'); } printf("Showing data...\n"); @@ -25,6 +25,26 @@ int main() printf("Showing data...\n"); show(); + printf("Adding data at begin and end...\n"); + + for (idx = 0; idx < 10; idx++) + { + if (idx % 2 == 0) + add(idx, 1); + else + add(idx, 0); + } + + addPos(22, 5); + addPos(22, 40); + addPos(22, -10); + + printf("Showing data...\n"); + show(); + + printf("Clearing data...\n"); + clear(); + printf("Done\n"); return 1;