Reputation: 15
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;
}
Upvotes: 0
Views: 91
Reputation: 66194
You build a forward-chained linked list in input order by constantly updating a target point on which to hang the next node.
head
pointer.next
pointer of that previous node.next
pointer is set to NULL and you're finished.It may sound complicated, but utilizing a pointer-to-pointer makes the algorithm surprisingly simple, efficient, and requires no special tedious case for testing for a null head pointer that will only ever be true once. Including added error checking
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
int main()
{
node *head = NULL;
node **pp = &head;
int num;
printf("how many nodes\r\n");
if (scanf("%d", &num) == 1 && num > 0)
{
for (int i=0; i<num;)
{
int value;
printf("enter node %d = ", i+1);
if (scanf("%d", &value) == 1)
{
*pp = malloc(sizeof **pp);
if (*pp == NULL)
{
perror("Failed to allocate new list node");
exit(EXIT_FAILURE);
}
// hang the new node
(*pp)->data = value;
// setup pp to hold address of next pointer
// to populate on the next iteration.
pp = &(*pp)->next;
// next node
++i;
}
else
{
int c;
while ((c = fgetc(stdin)) != EOF && c != '\n');
}
}
// terminate the list
*pp = NULL;
}
printf("linked list formed is:\n");
for (const node *p = head; p != NULL; p = p->next)
{
printf(" p->data = %d\n", p->data);
}
// free the list
while (head)
{
node *p = head;
head = head->next;
free(p);
}
return 0;
}
Sample Run
how many nodes
5
enter node 1 = 1
enter node 2 = 3
enter node 3 = 5
enter node 4 = 7
enter node 5 = 9
linked list formed is:
p->data = 1
p->data = 3
p->data = 5
p->data = 7
p->data = 9
Upvotes: 2
Reputation: 356
A change in code by Mohammed Meraj
to create the list in the correct order.
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
int main()
{
int i, num, value;
node *p = NULL;
node *head = NULL;
node *q = 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;
// Form links
if(!q) head = q = p;
else{ q->next = p; q = p; }
}
printf("linked list formed is \n");
for(p = head ; p != NULL ; p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
// Freeing memory to avoid mem leaks
while(head != NULL)
{
p = head;
head = head->next;
free(p);
}
return 0;
}
if you want to do the code with only head and p, it can be done too
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
int main()
{
int i, 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);
if(!head){
head = (node *)malloc(sizeof(node));
head->data = value;
head->next = NULL;
p = head;
}else{
p->next = (node *)malloc(sizeof(node));
p->next->data = value;
p->next->next = NULL;
p = p->next;
}
}
printf("linked list formed is \n");
for(p = head ; p != NULL ; p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
// Freeing memory to avoid mem leaks
while(head != NULL)
{
p = head;
head = head->next;
free(p);
}
return 0;
}
Upvotes: 0
Reputation: 116
You are just updating the head first time and not creating any links please find the fixed code below
#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;
// Form links
p->next = head;
head = p;
}
printf("linked list formed is \n");
for(p = head ; p != NULL ; p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
// Freeing memory to avoid mem leaks
for(p = head ; head != NULL ; head = head->next)
{
p = head;
free(p);
}
return 0;
}
You can refer to my library for a more generic implementation of link_list
Upvotes: 1