cdata-structureslinked-listdoubly-linked-listturbo-c++

I am Working with Doubly Linked Lists in C and I am Using Turbo C++ but The Compiler is Taking Two Additional Nodes without Adding


I am Working With Doubly Linked List & Implementing Them using C I am using Turbo C++ as my Compiler But it's Taking Two Constant Additional Nodes Every Time without Writing Code for It The Same Code is Running in VS Code But I Should Run it In Turbo C++ I tried Changing Systems, but it didn't Work

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

struct Node
{
    struct Node *prev;
    int data;
    struct Node *next;
} *head = NULL, *temp = NULL;

void insatbeg()
{
    int item;
    struct Node *ptr = NULL;
    printf("\nEnter Item: ");
    scanf("%d", &item);
    ptr = (struct Node *)malloc(sizeof(struct Node *));
    if (ptr == NULL)
        printf("\nOverflow Occured");
    else if (head == NULL)
    {
        ptr->data = item;
        ptr->next = ptr->prev = NULL;
        head = ptr;
    }
    else
    {
        ptr->prev = NULL;
        ptr->data = item;
        ptr->next = head;
        head->prev = ptr;
        head = ptr;
    }
}

void display()
{
    if (head == NULL)
        printf("\nList is Empty");
    else
    {
        temp = head;
        while (temp != NULL)
        {
            printf("%d\t", temp->data);
            temp = temp->next;
        }
    }
}

int main()
{
    int loopvar = 1, switchvar;
    clrscr();
code:
    while (loopvar == 1)
    {
        printf("\nEnter 1 to Insert at First");
        printf("\nEnter 2 to Display");
        printf("\nEnter: ");
        scanf("%d", &switchvar);
        switch (switchvar)
        {
        case 1:
            insatbeg();
            break;
        case 2:
            display();
            break;
        default:
            printf("\nEnter Properly: ");
            goto code;
            break;
        }
        printf("\nDo You Want to Continue: ");
        scanf("%d", &loopvar);
    }
    getch();
}
'''

Also the Redundant Nodes Added Always are Constant


Solution

  • This memory allocation

    ptr = (struct Node *)malloc(sizeof(struct Node *));
    

    is invalid. You allocated a memory for the pointer type struct Node * while you need to allocate memory for an object of the type struct Node. So write

    ptr = (struct Node *)malloc(sizeof(struct Node));
    

    Also in this code snippet

    else
    {
        ptr->prev = NULL;
        ptr->data = item;
        ptr->next = head;
        head = ptr;
    }
    

    you forgot to set the data member prev of the head node. Write

    else
    {
        ptr->prev = NULL;
        ptr->data = item;
        ptr->next = head;
        head->prev = ptr;
        head = ptr;
    }
    

    Pay attention to that the pointer temp1 is not used in your program.

    If the code does not work as expected using the compiler Turbo C++ then try to initialize the pointer to the head node explicitly

    struct Node
    {
        struct Node *prev;
        int data;
        struct Node *next;
    } *head = NULL, *temp, *temp1;