0

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;
}
4
  • 3
    in the first for loop after scanning, you are not connecting the node with the previous nodes Commented Aug 10, 2021 at 9:41
  • @susanth29 I am not understanding how to connect them .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? Commented Aug 10, 2021 at 9:45
  • save the last element you added separately and then add the new element accordingly. Commented Aug 10, 2021 at 9:47
  • Why do you think it should not only print 11? Commented Aug 10, 2021 at 10:52

3 Answers 3

2

You build a forward-chained linked list in input order by constantly updating a target point on which to hang the next node.

  • Initially that pointer is the head pointer.
  • The next node will be hung on the next pointer of that previous node.
  • When done, the last 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
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

That's because we are adding elements to Head /begining
Then I think we should use another pointer q to create the list in correct order.
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.