I want to create integer linked list and display. Suppose there are 3 nodes with values 11,22,33 . But when I display it, its printing only 1st value i.e. 11 . What is going wrong?
NOTE : To create and display linked list , Whether head and p node variable are enough or is it must to take 3 node pointer variables . i.e. head , p and q also?
#include <stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
int main()
{
int i, j, num, value;
node *p = NULL;
node *head = NULL;
printf("how many nodes\r\n");
scanf("%d",&num);
for(i = 0 ;i < num ; i++)
{
printf("enter node %d = ",i+1);
scanf("%d",&value);
p = (node *)malloc(sizeof(node));
p->data = value;
p->next = NULL;
if(head == NULL)
{
head = p;
}
}
printf("linked list formed is \r\n");
for(p = head ; p != NULL ; p = p->next)
{
printf("p->data = %d\r\n ",p->data);
}
return 0;
}