gpt4 book ai didi

c - 双向链表 C - 变量 'list' 周围的堆栈已损坏

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:53 25 4
gpt4 key购买 nike

我正在编写一个代码,将双向链表 list 分成两个列表 listAlistB 并将它们打印出来。代码似乎可以完成工作,但最终程序崩溃了。调试器抛出运行时检查失败 #2 - 变量“listA”周围的堆栈已损坏。运行时检查失败 #2 - 变量“list”周围的堆栈已损坏。 我读到这可能是因为没有为我的结构分配足够的内存,但我应该分配多少?

完整代码:

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

typedef struct node {
int val;
struct node* prev;
struct node* next;
}Node;
typedef struct list {
Node* head;
Node* tail;
}List;

void init(List* l) {
l->head = NULL;
l->tail = NULL;
}
Node* create(int val) {
Node* ptr = (Node*)malloc(sizeof(Node));
ptr->val = val;
ptr->next = NULL;
ptr->prev = NULL;

return ptr;
}
void printList(const List* list) {

Node *ptr = list->head;
while (ptr != NULL) {
printf("%i ", ptr->val);
ptr = ptr->next;
}
puts("");
free(ptr);
}
void pushLast(List* l, Node* node) {

if (l->head == NULL) {
l->head = node;
l->tail = node;
}
else {
node->prev = l->tail;
l->tail->next = node;
l->tail = node;
}
}
void splitList(const List* list) {

List* listA;
List* listB;
init(&listA);
init(&listB);

Node* ptr = list->head;
int i = 0;
while (ptr != NULL) {

Node* node = create(ptr->val);
if (i % 2 == 0)
pushLast(&listA, node);
else
pushLast(&listB, node);
i++;
ptr = ptr->next;
}

puts("Input list");
printList(list);
puts("Odd nodes list:");
printList(&listA);
puts("Even nodes list:");
printList(&listB);
}

int main(void) {

List* list;
init(&list);

int i;
for (i = 1; i <= 10; i++) {
Node* node = create(i);
pushLast(&list, node);
}
splitList(&list);
return 0;
}

收到的输出:

Input list:
1 2 3 4 5 6 7 8 9 10
Odd nodes list:
1 3 5 7 9
Even nodes list:
2 4 6 8 10

欢迎任何帮助。

最佳答案

首先,您没有为要指向的指针 listlistAlistB 分配内存。

其次,您将 listlistAlistB 定义为 List *。然后将 &list&listA&listB - 类型为 List ** - 传递给你的函数,而你函数需要 List *

您需要在 splitList() 中进行以下更改。 (仅显示有错误的代码的相关部分 - 或者需要添加 malloc):

void splitList(const List* list) 
{
List *listA;
List *listB;

/* Allocate memory to which these pointers will point */
if ((listA = malloc(sizeof(List))) == NULL) {
/* Error handling code */
exit(1);
}
if ((listB = malloc(sizeof(List))) == NULL) {
/* Error handling code */
exit(1);
}

/* ... */
while (ptr != NULL)
{
Node* node = create(ptr->val);
if (i % 2 == 0)
/* pushLast(&listA, node); */ /* ISSUE here */
pushLast(listA, node);
else
/* pushLast(&listB, node); */ /* ISSUE here */
pushLast(listB, node);
i++;
ptr = ptr->next;
}

/* ... */

puts("Odd nodes list:");
/* printList(&listA); */ /* ISSUE here */
printList(listA);
free(ListA); /* Free 1 */
puts("Even nodes list:");
/* printList(&listB); */ /* ISSUE here */
printList(listB);
free(ListB); /* Free 2 */
}

此外,您需要在 main 中进行类似的更改:

int main(void)
{
List* list;

/* Allocate memory */
if((list = malloc(sizeof(List))) == NULL) {
/* Error handling code */
exit(1);
}

/* init(&list); */ /* ISSUE here */
init(list);

int i;
for (i = 1; i <= 10; i++)
{
Node* node = create(i);
/* pushLast(&list, node); */ /* ISSUE here */
pushLast(list, node);
}
/* splitList(&list); */ /* ISSUE here*/
splitList(list);
free(list); /* free 3 */

return 0;
}

另请注意,您需要正确释放所有使用malloc 分配的内存以避免内存泄漏。可以看出,您没有释放所有节点。您拥有的freed只是printList中的一个节点() 函数。

关于c - 双向链表 C - 变量 'list' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38851113/

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