KHAN SAHAB
KHAN SAHAB

Reputation: 21

Code of display node in linked list doesn't work

I wrote code of insertion in linked list. But when I run this code, my display function doesn't work properly and print something else.

**ERROR IN CODE**

Here, I declare the head function as global variable and set it to NULL. After that when I return to main function and goes through it. after some code one node is assigned to structure p and the first node is declare as head (now our head is not NULL, it contains some value in node), then we called the display function. Now when we return to the display function and run it, it shows that our head is NULL and not NULL both.

BELOW IS MY CODE

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

struct node
{
    int data;
    struct node* next;
};
struct node* head = NULL, * tail = NULL;

void printList()
{
    struct node* ptr = head;
    if (head == NULL)
    {
        printf("Empty list\n");
    }
    printf("Nodes of the list are: \n");
    while (ptr != NULL)
    {
        printf("%d \t", ptr->data);
        ptr = ptr->next;
    }
    printf("\n");
}

struct node* insertion(struct node** head, struct node** tail, int ele, int pos)
{
    int i;
    struct node* new_node, * temp;
    new_node = (struct node*)malloc(sizeof(struct node));
    if (new_node == NULL)
    {
        printf(" Dynamic memory allocation failed. Insertion can not be done");
        return NULL;
    }
    else
    {
        new_node->data = ele;
        if (pos < 1)
        {
            printf(" Inappropriate position");
            return NULL;
        }
        else if (*head == NULL)
        {
            new_node->next = NULL;
            *head = *tail = new_node;
            return (new_node);
        }
        else if (pos == 1)
        {
            new_node->next = *head;
            *head = new_node;
            return(new_node);
        }
        else
        {
            temp = *head;
            i = 1;
            while (i < pos - 1 && temp != NULL)
            {
                temp = temp->next;
                i++;
            }
            if ((temp == NULL) || ((i == pos - 1) && (temp->next == NULL)))
            {
                new_node->next = NULL;
                (*tail)->next = new_node;
                (*tail) = new_node;
                return (new_node);
            }
            else
            {
                new_node->next = temp->next;
                temp->next = new_node;
                return (new_node);
            }
        }
    }

}


int main()
{
    struct node* head, * tail, * p;
    int n, i, ele, pos;

    do {
        printf(" Enter the number of elements initially in linked list:   ");
        scanf("%d", &n);
        if (n < 0)
        {
            printf(" Enter the number greater than zero \n");
        }
    } while (n < 0);
    printf(" Enter the elements of linked list \n");
    for (i = 0; i < n; i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        printf("%d element : ", i + 1);
        scanf("%d", &p->data);
        if (head == NULL)
        {
            head = p;
            tail = p;
        }
        else
        {
            tail->next = p;
            tail = p;
        }
    }

    printList();

    printf(" Enter the value which you want to insert:  ");
    scanf("%d", &ele);

    printf(" Enter the position at which you want to insert:  ");
    scanf("%d", &pos);

    insertion(&head, &tail, ele, pos);
    return 0;
}

Upvotes: 0

Views: 146

Answers (1)

Ray
Ray

Reputation: 2003

int main()
{
    struct node* head, * tail, * p;

This is your problem. You're declaring local variables in main named head and tail that hide the globals while in that scope. When you assign to head and tail later in main, you're changing the value of the locals. The globals remain unchanged.

Upvotes: 0

Related Questions