Added clear and add

This commit is contained in:
2016-02-17 11:24:27 +01:00
parent ab8f50d78e
commit a852fbc991
3 changed files with 46 additions and 4 deletions
+31 -4
View File
@@ -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");
}
+1
View File
@@ -5,6 +5,7 @@ struct node
{
int data;
struct node *next;
struct node *previous;
};
void init();
+14
View File
@@ -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;
}