简体   繁体   English

我正在使用 C 中的双向链接列表,并且我正在使用 Turbo 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 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++我尝试更改系统,但它没有工作

'''
#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->next->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 Disply");
        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();
}
'''

Should I consider it as Compiler Fault我是否应该将其视为编译器错误

This memory allocation本memory配置

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 .您为指针类型struct Node *分配了 memory 而您需要为类型struct Node的 object 分配 memory。 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.您忘记设置头节点的数据成员prev 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.请注意,您的程序中没有使用指针temp1

If the code does not work as expected using the compiler Turbo C++ then try to initialize the pointer to the head node explicitly如果使用编译器 Turbo C++ 代码无法按预期工作,则尝试显式初始化指向头节点的指针

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用turbo C ++,我的程序没有产生输出 - I am using turbo C++ and my program is producing no output 当我使用大量操作时,在C中使用双链表的队列被杀死-程序被杀死 - Queue using doubly linked list in c - program is killed when I am using large number of operations 我在 Turbo C++ 中使用 struct — 它给出的错误声明是不允许的 - I am using struct in Turbo C++ — it is giving error declaration is not Allowed Here 我在C中实现链接列表,我的addhead功能无法正常工作。 怎么了? - I am implementing linked lists in C, and my addhead function is not working correctly. What's wrong? 我是否为C中的双向链接列表正确实现了此删除节点功能? - Am I implementing this remove node function for a doubly linked list in C correctly? 我在 C 中实现一个双向链表,并在销毁 function 中出现分段错误 - I am implementing a doubly linked list in C, and having segmentation fault in a destroy function 在 C 中使用双向链表的问题 - Issue working with Doubly-linked Lists in C C中的链接列表:我为什么要进行分类? - Linked list in C: Why am I segfaulting? 使用Turbo C ++,我如何在C中绘制图形? - Using Turbo C++, how I can draw graphics in C? 在C中的链接链接中添加/删除节点 - Adding/Deleting nodes in a Linked LIsts in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM