gpt4 book ai didi

c - LinkedList 堆栈损坏

转载 作者:行者123 更新时间:2023-11-30 15:35:55 25 4
gpt4 key购买 nike

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
typedef struct listnode{
int item;
struct listnode *next;
}ListNode;

typedef struct _linkedlist{
ListNode *head;
int size;
} LinkedList;

void printList(LinkedList *ll);
int sizeList(LinkedList *ll);
int insertSorted(LinkedList *ll, int value);
int removeDuplicates(LinkedList *ll);

int main()
{
int choice, i = 0;
ListNode *temp=NULL;
LinkedList *ll=NULL;

printf("1. create LinkedList\n2. insertSorted\n3. removeDuplicates\nChoose an option: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter a list of numbers, terminated by the value -1: ");
scanf(" %d", &i);
while (i != -1){
if (ll == NULL)
{
ll = malloc(sizeof(LinkedList));
temp = ll;
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;

}
temp->item = i;
scanf(" %d", &i);
}
temp->next = NULL;
printList(&ll);
printf("Size of linked is %d", sizeList(&ll));
break;
case 2:

default:
break;
}
}

void printList(LinkedList *ll)
{
ListNode *temp = ll->head;
if (temp == NULL)
return;
while (temp!=NULL)
{
printf("%d ", temp->item);
temp = temp->next;
}
printf("\n");
}

int sizeList(LinkedList *ll)
{
int size=0;

ListNode *temp = ll->head;
if (temp == NULL)
return 0;

while (temp != NULL)
{
size++;
ll->size = size;
temp = temp->next;
}
return ll->size;
}

我想创建一个链表并计算链表的大小并输出。我设法获取大小并打印出列表,但最后,我的程序显示调试错误并指出运行时检查失败#2 - 变量“ll”周围的堆栈已损坏。我可以知道为什么会发生这种情况吗?

最佳答案

其中一个不正确的地方是您的 main() 函数。

if (ll == NULL)
{
ll = malloc(sizeof(LinkedList));
temp = ll; // <- This is incorrect!!
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}

temp 是一个ListNode,如何为其分配一个LinkedList?同样,对于这一行,templl 都指向同一内存,并且在其中一个上进行操作将覆盖其他。

它可能应该是这样的:

if (ll == NULL)
{
ll = malloc(sizeof(LinkedList));
temp = malloc(sizeof(ListNode));
temp->next = NULL;
ll->head = temp;
ll->size = 1;
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}

但这也没有想象中那么美好。我更愿意:

int main()
{
int choice, i = 0;
ListNode *temp=NULL;
LinkedList ll = { NULL, 0 };

printf("1. create LinkedList\n2. insertSorted\n3. removeDuplicates\nChoose an option: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter a list of numbers, terminated by the value -1: ");
scanf(" %d", &i);
while (i != -1){
if (ll.head == NULL)
{
temp = malloc(sizeof(ListNode));
ll.head = temp;
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
scanf(" %d", &i);
}
temp->next = NULL; // <- Please take a second look at this line. What happens if your first entry is -1?
printList(&ll); // <- This too...
printf("Size of linked is %d", sizeList(&ll)); // <- and this as well...
break;
case 2:

default:
break;
}
}

这是因为不需要动态分配LinkedList。只要程序正在运行,它就一直存在,因此将其放在堆上是没有意义的。

您的代码还存在其他问题,我建议您使用橡皮鸭代码,并强烈建议阅读 this link on debugging

关于c - LinkedList 堆栈损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22753296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com