304 Intermediate C Programming
{
14
return ;15
}16
List_destroy2(head -> next);17
free (head); // must be after the recursive call18
}19
The following main function shows how to use the linked list functions we have developed
in this chapter.
// file: main.c
1
#include "list.h"2
#include <stdlib.h>3
#include <stdio.h>4
int main(int argc , char * argv[])5
{6
Node * head = NULL; /* must initialize it to NULL */7
head = List_insert(head, 917);8
head = List_insert(head, -504);9
head = List_insert(head, 326);10
List_print(head);11
head = List_delete(head, -504);12
List_print(head);13
head = List_insert(head, 138);14
head = List_insert(head, -64);15
head = List_insert(head, 263);16
List_print(head);17
if (List_search(head, 138) != NULL)18
{19
printf("138isinthelist
");20
}21
else22
{23
printf("138isnotinthelistn");24
}25
if (List_search(head, 987) != NULL)26
{27
printf("987isinthelist
");28
}29
else30
{31
printf("987isnotinthelistn");32
}33
head = List_delete(head, 263); // delete the first Node34
List_print(head);35
head = List_delete(head, 917); // delete the last Node36
List_print(head);37
List_destroy(head); // delete all Nodes38
return EXIT_SUCCESS;39
}40
The output of this program is: