2017-10-13 116 views
-2

我做了一个函数打印一个BTree在水平顺序没有递归的方式。运行时错误c项目

和我有一个问题找到我的错误..出现以下问题。

运行时检查失败#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); 

    } 

} 
+1

你以某种方式覆盖内存,但是你可能会这样做的所有功能都不在你的例子中。 –

+0

请制作[MCVE](强调__minimal__)。 –

+0

我添加一个链接到所有的功能和数据结构... –

回答

0

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

您的代码应采取的

  • 照顾您使用的PQ功能,以错误的方式多的错误,你传递一个指针的指针,而不是原来的指针,所以你改写代码
  • create_queue应该分配,除非你把它的init但是这不是主要的问题
  • 您应该检查是否create_queue成功
  • 要保存在队列中哪些是队列*为INT这是错误的,而不是便携式的架构各色地址NT比32位
  • 要分配电流中的一个节点(节点树结构)一queue_element元素指针结构,这也是不正确的,因为它们是不同的类型和结构

请在这些问题上工作,如果你想要更多的细节请联系我 我会很乐意帮助