gpt4 book ai didi

c - 运行时错误 - c 项目

转载 作者:行者123 更新时间:2023-11-30 20:32:56 29 4
gpt4 key购买 nike

我创建了一个函数,以非递归方式按级别顺序打印 BTree。

我无法找到我的错误..出现以下问题。

运行时检查失败#2 - 变量“pq”周围的堆栈已损坏。 如果有人能告诉我问题出在哪里,或者我下次如何自己找到它......?如果需要,我会添加完整的项目。 enter link description here

void PrintTreeLevelOrder(bstree tree){      //The problem some where here.....
queue *pq = (queue*)malloc(sizeof(queue)); // is struct of : *front, *rear

node *current;// is struct of : root
create_queue(&pq);//create queue- items_num = 0,front = NULL,rear = NULL

if (tree.root == NULL) {
printf("Your Tree Is Empty:\n");
return;
}
current = tree.root;
enqueue(current, &pq);
printf("Your Tree Displayed As Queue:\n");
while ((size_of_queue(&pq) )!=0) {
current = pq->front;
printf("%d ", current->data);
if (current->left != NULL)
enqueue(current->left, &pq);

if (current->right)
enqueue(current->right, &pq);
dequeue(&pq, &current);

}

}

最佳答案

首先,我想说你的算法是正确的,请阅读以下内容。

您的代码有多个需要注意的错误

  • 您以错误的方式使用了 pq 函数,您传递了一个指向指针的指针而不是原始指针,因此您覆盖了代码
  • Create_queue 应该分配,除非你调用它 init,但这不是主要问题
  • 您应该检查 create_queue 是否成功
  • 您将队列中的地址保存为 int,这是错误的,并且对于非 32 位的体系结构不可移植
  • 您正在为当前节点(节点树结构)分配一个queue_element元素指针结构,这也是不正确的,因为它们是不同的类型和架构

请注意以下几点,如果您想了解更多详细信息,请与我联系我很乐意提供帮助

关于c - 运行时错误 - c 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46730875/

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