Hi
I have created a simple linked list program but it is printing only the first element of list.
The code is as follow:
Code:
#include<stdio.h>
#include<stdlib.h>
struct link
{
int data;
struct link *next;
};
int main()
{
struct link *head;
struct link *temp,*node;
node = NULL;
temp = NULL;
head = NULL;
int ch = 1;
while(ch)
{
printf("\n Enter your Choice");
printf("\n 1: Insert");
printf("\n 2: Display");
printf("\n 3: Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
int info;
printf("\n Enter the number");
scanf("%d",&info);
node = (link *)malloc(sizeof(link));
if( head == NULL )
{
node->data = info;
node->next = NULL;
head = node;
}
else
{
temp = head;
while( temp->next != NULL )
temp=temp->next;
node->data = info;
node->next = NULL;
temp = node;
}
break;
case 2:
temp = head;
while ( temp !=NULL ){
printf("\n %d",temp->data);
temp = temp->next;
}
break;
case 3:
exit(0);
break;
}
}
}
Can any body tell me where is the problem?
Thanks