Program: Creation of a linked list
In the next example, wewill look to the process of addition of new nodes to the list with the function create_list().
#include
#include
#define NULL 0
structlinked_list
{
int data;
structlinked_list *next;
};
typedefstructlinked_list list;
void main()
{
list *head;
void create(list *);
int count(list *);
void traverse(list *);
head=(list *)malloc(sizeof(list));
create(head);
printf(" \n traversing the list \n");
traverse(head);
printf("\n number of elements in the list %d \n", count(head));
}
void create(list *start)
{
printf("input term -1111to getting out of the loop\n");
scanf("%d", &start->data);
if(start->data == -1111)
start->next=NULL;
else
{
start->next=(list*)malloc(sizeof(list));
create(start->next);
}
}
void traverse(list *start)
{
if(start->next!=NULL)
{
printf("%d --> ", start->data);
traverse(start->next);
}
}
int count(list *start)
{
if(start->next == NULL)
return 0;
else
}
return (1+count(start->next));