【发布时间】:2017-10-13 13:15:54
【问题描述】:
我创建了一个以非递归方式按级别顺序打印 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, ¤t);
}
}
【问题讨论】:
-
您以某种方式覆盖了内存,但是您可能这样做的所有功能都没有出现在您的示例中。
-
请发minimal reproducible example(强调最小)。
-
我添加了所有函数和数据结构的链接...
-
提高编译器的警告级别。认真对待编译器的警告并修复代码,直到不再发出警告。不要不要盲目地“抛弃”警告!
-
对于初学者:这个
create_queue(&pq);看起来不太好。我很确定编译器已经警告过你了。同样在这里:size_of_queue(&pq)
标签: c pointers data-structures