1

This is my program in C which always inserts into a linked list at the end. But when I try to print the list elements, nothing is displayed. Here is the code :

#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};
void insert(struct Node *, int);
int main(void)
{
    struct Node *head = NULL, *current;
    int n, i, x, data;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &data);
        insert(head, data);
    }
    current = head;
    while(current != NULL)
    {
        printf("%d ", current->data);
        current = current->next;
    }
}
void insert(struct Node *head, int data)
{
    struct Node *newnode, *current = head;
    newnode = (struct Node *)malloc(sizeof(struct Node));
    newnode->data = data;
    newnode->next = NULL;
    if(head == NULL)
    {
        head = newnode;
    }
    else
    {
        while(current->next != NULL)
        {
            current = current->next;
        }
        current->next = newnode;
    }
}

I cannot understand what might be the issue. Please help.

Rahul
  • 143
  • 1
  • 17

5 Answers5

3

Your insert cannot modify head. Change it to

void insert(struct Node **head, int data)

and change it by

*head = newnode;

and call it like this

insert(&head, data);

LoztInSpace
  • 5,584
  • 1
  • 15
  • 27
2

You need to pass the reference of the head pointer, then only the changes made to it will be visible.

You must declare your function like

void insert(struct Node **, int);

and also call it like

insert(&head, data);

also, make changes to function definition

void insert(struct Node **head, int data)
{
    struct Node *newnode, *current = *head;
    newnode = (struct Node *)malloc(sizeof(struct Node));
    newnode->data = data;
    newnode->next = NULL;
    if(*head == NULL)
    {
        *head = newnode;
    }
    else
    {
        while(current->next != NULL)
        {
            current = current->next;
        }
        current->next = newnode;
    }
}
Tanuj Yadav
  • 1,259
  • 13
  • 21
2

Here, while you are passing the head pointer to your insert() function, it is not being updated in your main() function.

So, either declare your head pointer as global or return your head pointer and update it in your main() function.

In the below code I had taken the head pointer as global and removed the head pointer as your parameter from the insert() function.

Here is the code :-

#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};
struct Node *head=NULL;
void insert(int);
int main(void)
{
    struct Node  *current;
    int n, i, x, data;
    clrscr();
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
    scanf("%d", &data);
    insert(data);
    }
    current = head;
    while(current != NULL)
    {
    printf("%d \n", current->data);
    current = current->next;
    }
    getch();
    return 0;
}
void insert(int data)
{
    struct Node *newnode, *current = head;
    newnode = (struct Node *)malloc(sizeof(struct Node));
    newnode->data = data;
    newnode->next = NULL;
    if(head == NULL)
    {
    head = newnode;
    }
    else
    {
    while(current->next != NULL)
        {
            current = current->next;
        }
        current->next = newnode;
    }
}
1

You need to pass the head by reference as you are making changes to it that should be visible.

insert(head, data);

should become

insert(&head, data);

Also the function signature will change.

void insert(struct Node *head, int data)

should become

void insert(struct Node **head, int data)

Also make appropriate changes in the function.

Like,

current = *head;
รยקคгรђשค
  • 1,919
  • 1
  • 10
  • 18
0

Because you are passing the pointer by value. The function operates on a copy of the pointer, and never modifies the original.

Either pass a pointer to the pointer (i.e. a struct head **), or instead have the function return the pointer.

You can try running the following code which will give the output as null

printf("%s",head);
while(current != NULL)
{
    printf("%d", current->data);
    current = current->next;
}
  • type of `head` is `struct Node *`, not `char*`. – BLUEPIXY Jul 28 '17 at 04:41
  • If you want to display pointer address, `printf("head is %p\n", (void*)head);`. If you want to confirm that it is just `NULL`, `if(head == NULL) puts("head is NULL");` – BLUEPIXY Jul 28 '17 at 04:49