为什么程序崩溃了这段代码? [英] Why program is crashing for this code?

查看:61
本文介绍了为什么程序崩溃了这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用队列制作了显示二进制搜索树的功能,但无法弄清楚我做错了什么。

I made a function for display binary search tree breadth wise using a queue, but can't able to figure out what I am doing wrong.

#include <iostream>

using namespace std;

class Bstnode
{
    public:
    int data;
    Bstnode* left;
    Bstnode* right;
};

class Node
{
public:
    Bstnode *addr;
    Node *next;
};

Node *head=NULL;
void Enqueue(Bstnode *ptr);
void Dequeue();
Bstnode* top();
void Displayq();

Bstnode *root=NULL;
void AddLeaf(Bstnode *&root, int num);
void Display(Bstnode *root);
bool Search(Bstnode *root, int num);
void BdtDisplay(Bstnode *root);

int main()
{
    AddLeaf(root,5);
    AddLeaf(root,15);
    AddLeaf(root,2);
    AddLeaf(root,3);
    AddLeaf(root,8);
    AddLeaf(root,20);
    AddLeaf(root,1);
    AddLeaf(root,6);
    AddLeaf(root,7);
    BdtDisplay(root);
    return 0;
}

void Enqueue(Bstnode *ptr)
{
    Node *temp= new Node;
    temp->addr=ptr;
    temp->next=NULL;
    if(!temp)
        cout<<"\nnew node not created";
    if(head==NULL)
        head=temp;
    else
    {
        Node *temp1=head;
        while(temp1->next!=NULL)
            temp1=temp->next;
        temp1->next=temp;
    }
}
void Dequeue()
{
    if(head==NULL)
        cout <<"\nEmpty queue :P"<<endl;
    else
        {
            Node *temp=head;
            head=head->next;
            delete temp;
        }

}
void Displayq()
{
    int c(0);
    if(head==NULL)
        cout<<"\nno data in queue"<<endl;
    else
    {

        Node *temp=head;
        cout<<"\n";
        while(temp!=NULL)
        {
            cout<<temp->addr<<" ";
            temp=temp->next;
            c++;
        }
        cout<<"\nNo. of element in queue= "<<c<<endl;
    }
}
Bstnode* top()
{
    if(head==NULL)
        return 0;
    else
        return head->addr;
}
void AddLeaf(Bstnode *&root,int num)
{
    if(root==NULL)
    {
        Bstnode *temp = new Bstnode;
        temp->data=num;
        temp->left=NULL;
        temp->right=NULL;
        root=temp;
    }
    else if(num > root->data)
        AddLeaf(root->right,num);
    else
        AddLeaf(root->left, num);
}

bool Search(Bstnode *root, int num)
{
    if(root==NULL)
        return false;
    if(root->data==num)
        return true;
    if(num>root->data)
        return Search(root->right,num);
    else
        return Search(root->left,num);
}

void Display(Bstnode* root)
{
    if(root==NULL)
        return;
    if(root->left!=NULL)
        Display(root->left);
     cout << root<<" contain value: "<<root->data<<" "<<"\n";
    if(root->right!=NULL)
        Display(root->right);
}

void BdtDisplay(Bstnode *root)
{
    if(root==NULL)
        return;
    else
    {
        Dequeue();


        if(root->left!=NULL)
        {
            cout<<root->left->data<<" ";
            Enqueue(root->left);

        }
        if(root->right!=NULL)
        {
            cout<<root->right->data<<" ";
//crash happen right here during second iteration during enqueue
            Enqueue(root->right); 

        }


        BdtDisplay(top());
    }

}

推荐答案

首先从调试器开始,然后按照它进行操作。

main 函数的第一行放置一个断点,并运行你的应用程序。

一步一步。

你的程序崩溃了吗?

如果是这样,再次启动它,这次进入AddLeaf函数,然后逐步执行。您可以使用调试器检查变量并准确计算出发生的情况,并在实际执行每一行之前确定您希望发生的事情。它是否完全符合您的预期?如果没有,为什么不呢?



继续观察和踩踏,最终你应该找到它崩溃的点,并知道导致崩溃的原因,至少有一些信息可以告诉我们它是如何崩溃的,以及导致它的原因。


但是目前我们所能做的就是这个过程 - 调试是一项只有通过练习才能发展的技能。所以最好在一个简单的程序上学习它,然后才能在一个复杂的大型程序中使用它! :笑:



试一试 - 你应该学到很多东西。
So start with the debugger, and follow it through.
Put a breakpoint in the first line of your main function, and run your app.
Step over the line.
Did your program crash?
If so, start it again, and this time step into the AddLeaf function, and step through that. You can use the debugger to examine variables and work out exactly what is happening, and work out what you expect to happen before it you actually execute each line. Did it work out exactly as you expected? If not, why not?

Keep looking and stepping and eventually you should either find the point at which it crashes and have an idea what lead up to the crash, of at least have some information to tell us about how it crashed, and what led up to it.

But at the moment, all we could do is exactly that process - and debugging is a skill that you only develop by practicing. So it's best to learn it on a simple program like this before you need to use it on a big, complex one! :laugh:

Give it a go - you should learn a lot.


这篇关于为什么程序崩溃了这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆