katysha
katysha

Reputation: 139

How to iterate over linked list (c)

A simple linked list datatype called node contains a number and a pointer to the next node

the variable x is used to scanf capture values from user input

an array arr of to hold pointers to the node created

a pointer to the start node start_ptr

the first loop captures user input then appends it to the node and moves to the next node then the pointer for that node is added to arr

the last loop simply loops over the node start with the first node and prints each number captured from the user input

typedef struct node 
{
    int n;
    struct node *next ;
}
node;

int main(void)
{
    // variable to hold integers 
    int x ; 
    node arr[3];
    node *start_ptr;
    node *p = malloc(sizeof(node));
    // check if memory allocated for node 
    if (p == NULL)
    {
        return 1;
    }
    start_ptr = p;
    for (int i =0; i < 3; i++)
    {
        scanf("%i" , &x);
        p->n = x;
        p->next = realloc(p , sizeof(node));
        arr[i] = *p;
    }
    for (  node *tmp = start_ptr ; tmp != NULL; tmp = tmp->next)
    {
        printf("%i", tmp->n);
    }
    return 0;
}

what is wrong

when you run the code it accepts the three numbers from the user then starts printing out the last number until it crashes or you terminate the program

can anyone explain why the second loop is not working properly

Upvotes: 1

Views: 564

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50774

The code that iterates over the list is correct. The problems are all in the first for loop:

  1. The next pointer of the last node is not set to NULL.
  2. You use realloc when you should use malloc.
  3. You don't set p to the next element at the end of the loop.

You want this:

  ...
  start_ptr = p;
  for (int i = 0; i < 3; i++)
  {
    scanf("%i", &x);
    p->n = x;

    if (i == 3 - 1)     // set next to NULL for the last element
      p->next = NULL;   
    else
      p->next = malloc(sizeof(node));  // otherwise allocate memory for next element

    p = p->next;        // set p to next element
  }
  ...

You don't need the arr array. For debugging your code you'd better use your debugger.

Complete program:

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
  int n;
  struct node* next;
}
node;

int main(void)
{
  // variable to hold integers 
  int x;
  node* start_ptr;
  node* p = malloc(sizeof(node));   // check if malloc returns NULL
                                    // not necessary in toy programs
  start_ptr = p;
  for (int i = 0; i < 3; i++)
  {
    scanf("%i", &x);
    p->n = x;

    if (i == 3 - 1)     // set next to NULL for the last element
      p->next = NULL;
    else
      p->next = malloc(sizeof(node));  // otherwise allocate memory for next element

    p = p->next;        // set p to next element
  }

  for (node* tmp = start_ptr; tmp != NULL; tmp = tmp->next)
  {
    printf("%i", tmp->n);
  }
  return 0;
}

This code is far from perfect, it just corrects the bugs in the initial code. There is still room for further improvement.

Upvotes: 1

Related Questions