Added clear and add
This commit is contained in:
+31
-4
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ struct node
|
||||
{
|
||||
int data;
|
||||
struct node *next;
|
||||
struct node *previous;
|
||||
};
|
||||
|
||||
void init();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user