0

This is a simple program to insert a node and display it in a linked list bypassing the head node to the function. This is a part of a bigger program but I am unable to troubleshoot this. Each time I display the node it says the linked list is empty.

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

struct nodes
{
    int data;
    struct nodes *next;
};

typedef struct nodes *node;

void InsertFront(node head)
{
    int num;
    node temp = malloc(sizeof(node));
    printf("Enter The Value Of Node\n");
    scanf("%d", &num);
    temp->data = num;
    if (head == NULL)
    {
        temp->next = NULL;
        head = temp;
    }
    else
    {
        temp->next = head;
        head = temp;
    }
}

void Display(node head)
{
    node q;
    if (head == NULL)
    {
        printf("Linked List Seems To Be Empty");
    }
    else
    {
        q = head;
        while (q != NULL)
        {
            printf("%d -> ", q->data);
            q = q->next;
        }
    }
}

void main()
{
    node head;
    head = NULL;
    InsertFront(head);
    Display(head);
}
LeoVen
  • 632
  • 8
  • 18
Syed Umair
  • 65
  • 4

2 Answers2

0

First thing, it is not a good idea to typedef pointers. Take a look at this thread for a better discussion.

In order to change the head of your linked list you must pass it by reference and then change it inside the function. For better visualization, I ignored the typedef.

// Notice that now you need to pass a pointer to a pointer, not just a pointer
void InsertFront(struct nodes **head)
{
    int num;
    struct nodes *temp = malloc(sizeof(node));
    printf("Enter The Value Of Node\n");
    scanf("%d", &num);
    temp->data = num;

    if (*head == NULL)
    {
        temp->next = NULL;
        // by doing *head = temp we change the variable head declared in main
        *head = temp;
    }
    else
    {
        temp->next = *head;
        // Same thing here, we are updating the head by doing *head = ...
        *head = temp;
    }
}

And when you want to call the InsertFront function:

struct nodes *head = NULL;
InsertFront(&head); // notice the &
LeoVen
  • 632
  • 8
  • 18
0

Here is the working code with the errors fixed.

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

struct nodes
{
    int data;
    struct nodes *next;
};

typedef struct nodes *node;

void InsertFront(node *head)
{
    int num;
    node temp = (node)malloc(sizeof(node));
    printf("Enter The Value Of Node\n");
    scanf("%d", &num);
    temp->data = num;
    if (*head == NULL)
    {
        temp->next = NULL;
        *head = temp;
    }
    else
    {
        temp->next = *head;
        *head = temp;
    }
}

void Display(node head)
{
    node q;
    if (head == NULL)
    {
        printf("Linked List Seems To Be Empty");
    }
    else
    {
        q = head;
        while (q != NULL)
        {
            printf("%d -> ", q->data);
            q = q->next;
        }
    }
}

int main()
{
    node head;
    head = NULL;
    InsertFront(&head);
    Display(head);
}

You need to pass the head from main() as its address(using pointer to pointer). In the previous case, the head wasn't getting updated and due to this reason, it showed that the list is empty. Now, the node entered is getting correctly printed in this code.

Pratims10
  • 68
  • 6