【问题标题】:Linked List delete operation not working in visual studio using C链接列表删除操作在使用 C 的 Visual Studio 中不起作用
【发布时间】:2020-08-31 10:22:06
【问题描述】:

当我尝试在 Visual Studio 2019 中使用 c 实现链表时,它会产生堆错误。 这是由于免费功能。 但是,该代码在使用 GCC 编译器的在线编译器上运行良好。 https://www.jdoodle.com/c-online-compiler/

我想不通............

代码如下:

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

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

struct Node* head = NULL;

void append(int data)
{

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node*));
    (*newNode).data = data;
    (*newNode).next = NULL;

    if (head == NULL)
    {
        head = newNode;
        return;
    }

    struct Node* temp = head;

    while (temp->next != NULL)
    {
        temp = temp->next;
    }

    temp->next = newNode;
}

void insertAt(int position, int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node*));
    newNode->data = data;
    newNode->next = NULL;



    if (position == 1)
    {
        newNode->next = head;
        head = newNode;
        return;
    }

    struct Node* temp = head;

    for (int i = 1; i < position - 1; i++)
    {
        temp = temp->next;
    }

    newNode->next = temp->next;
    temp->next = newNode;
}

void deleteAt(int position)
{
    struct Node* temp = NULL;

    if (position == 1)
    {
        temp = head;
        head = temp->next;
        free(temp);
        return;
    }

    struct Node* tempHead = head;

    for (int i = 1; i < position - 1; i++)
    {
        tempHead = tempHead->next;
    }

    temp = tempHead->next;
    tempHead->next = temp->next;
    free(temp);


}

void print()
{
    struct Node* temp = head;

    while (temp != NULL)
    {
        printf("%d\n", temp->data);
        temp = temp->next;
    }

}

void main()
{
    append(3);
    append(4);
    append(5);
    append(6);

    insertAt(3, 20);
    insertAt(4, 50);
    insertAt(2, 70);

    deleteAt(4);
    deleteAt(3);


    print();
}

【问题讨论】:

    标签: c++ c visual-studio visual-c++


    【解决方案1】:

    只要在下面尝试一下这段代码。这段代码是我根据我的理解编写的,如果你对代码有任何问题,你可以进一步问我。你可以试试这段代码,或者只是用你的交叉检查.

    代码:

    链表:

    #include <stdio.h>
    #include <stdlib.h>
    struct Node
    {
     int data;
     struct Node *next;
    }*first=NULL;
    void create(int A[],int n)
    {
     int i;
     struct Node *t,*last;
     first=(struct Node *)malloc(sizeof(struct Node));
     first->data=A[0];
     first->next=NULL;
     last=first;
    
     for(i=1;i<n;i++)
     {
     t=(struct Node*)malloc(sizeof(struct Node));
     t->data=A[i];
     t->next=NULL;
     last->next=t;
     last=t;
     }
    }
    void Display(struct Node *p)
    {
     while(p!=NULL)
     {
     printf("%d ",p->data);
     p=p->next;
     }
    }
    void RDisplay(struct Node *p)
    {
     if(p!=NULL)
     {
     RDisplay(p->next);
     printf("%d ",p->data);
    
     }
    }
    int Delete(struct Node *p,int index)
    {
     struct Node *q=NULL;
     int x=-1,i;
    
     if(index < 1 || index > count(p))
     return -1;
     if(index==1)
     {
     q=first;
     x=first->data;
     first=first->next;
     free(q);
     return x;
     }
     else
     {
     for(i=0;i<index-1;i++)
     {
     q=p;
     p=p->next;
     }
     q->next=p->next;
     x=p->data;
     free(p);
     return x;
    
     }
    
    
    }
    int main()
    {
    
     int A[]={10,20,30,40,50};
     create(A,5);
    
     printf(“%d\n",Delete(first),2);
     Display(first);
    
     return 0;
    } 
    

    在主函数中,您可以传递在程序中创建的函数,也可以根据您的需要传递参数。

    【讨论】:

      【解决方案2】:

      正如@1201ProgramAlarm 回答的那样,分配大小是错误的。 sizeof(struct Node*) 是指针的大小,而不是 struct 的大小。

      不要尝试匹配类型,而是使用引用数据的大小。易于正确编码、审查和维护。

      C 中不需要强制转换。

      // struct Node* newNode = (struct Node*)malloc(sizeof(struct Node*));
      // instead... 
      //  ptr = malloc(sizeof *ptr * N);
      struct Node* newNode = malloc(sizeof *newNode);
      

      【讨论】:

        【解决方案3】:

        您传递给malloc 的尺寸有误。你应该通过sizeof(struct Node)

        如果您将其编译为 C++,则根本不应该使用 malloc

        【讨论】:

          猜你喜欢
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-06
          • 1970-01-01
          • 2017-01-16
          • 1970-01-01
          相关资源
          最近更新 更多