Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过引用传递指针(插入到二进制搜索树中)_C++ - Fatal编程技术网

C++ 通过引用传递指针(插入到二进制搜索树中)

C++ 通过引用传递指针(插入到二进制搜索树中),c++,C++,我的程序正在调用一个成员函数,该函数使用通过引用传递的指针(以便更改指针的值)将指针插入到二进制搜索树中。但是,当我试图在树中插入第二个节点时,程序崩溃。我假设这与一个坏指针有关 此赋值中唯一可以更改的部分是成员函数的实现,而不是声明 任何帮助都将不胜感激 //Header file using namespace std; struct PersonRec { char name[20]; int bribe; PersonRec* leftLink; Pe

我的程序正在调用一个成员函数,该函数使用通过引用传递的指针(以便更改指针的值)将指针插入到二进制搜索树中。但是,当我试图在树中插入第二个节点时,程序崩溃。我假设这与一个坏指针有关

此赋值中唯一可以更改的部分是成员函数的实现,而不是声明

任何帮助都将不胜感激

 //Header file
using namespace std;

struct PersonRec
{
    char name[20];
    int bribe;
    PersonRec* leftLink;
    PersonRec* rightLink;
};


class CTree
{

private:
    PersonRec *tree;
    bool IsEmpty();
    void AddItem( PersonRec*&, PersonRec*);
    void DisplayTree(PersonRec*);

public:
    CTree();
    //~CTree();
    void Add();
    void View();

};

//Implementation file

#include <iostream>
#include <string>

using namespace std;

#include "ctree.h"

CTree::CTree()
{
    tree = NULL;
}

//PersonList::~MyTree()
//{
//
//}


bool CTree::IsEmpty()
{
    if(tree == NULL) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

void CTree::Add()
{
    PersonRec* newPerson = new PersonRec();
    cout << "Enter the person's name: ";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.getline(newPerson->name, 20);
    cout << "Enter the person's contribution: ";
    cin >> newPerson->bribe;


    newPerson->leftLink = NULL;
    newPerson->rightLink = NULL;

    AddItem(tree, newPerson);
}

void CTree::View()
{
    if (IsEmpty())
    {
        cout<<"The list is empy";
    }
    else
    {
        DisplayTree(tree);

    }

};

void CTree::AddItem( PersonRec*& ptr, PersonRec* newPer )
{
        if (tree == NULL)
        {
            ptr = newPer;
        }
        else if ( newPer->bribe < ptr->bribe)
            AddItem(ptr->leftLink, newPer); 
        else
            AddItem(ptr->rightLink, newPer); 
}
void CTree::DisplayTree(PersonRec* ptr)
{
    if (ptr == NULL)
                    return;
    DisplayTree(ptr->rightLink);
    cout<<ptr->name<<" "<<"$"<<ptr->bribe <<endl;
    DisplayTree(ptr->leftLink); 
}


//Driver file
#include <iostream>

using namespace std;
#include <cstdlib>
#include "ctree.h"

int displayMenu (void);
void processChoice(int, CTree&);

int main (void)
{
int num;
CTree ct;
do 
{
num = displayMenu();
if (num != 3)
processChoice(num, ct);
} while (num != 3);
return 0;
}

int displayMenu (void)
{
int choice;
cout << "\nMenu\n";
cout << "==============================\n\n";
cout << "1. Add student to waiting list\n";
cout << "2. View waiting list\n";
cout << "3. Exit program\n\n";
cout << "Please enter choice: ";
cin >> choice;
return choice;
}

void processChoice(int choice, CTree& myTree)
{
   switch (choice)
   {
      case 1: myTree.Add (); break;
      case 2: myTree.View (); break;
   } 
}
//头文件
使用名称空间std;
结构PersonRec
{
字符名[20];
贿赂;
PersonRec*leftLink;
PersonRec*rightLink;
};
课堂教学
{
私人:
PersonRec*树;
bool是空的();
无效附加项(PersonRec*&,PersonRec*);
void DisplayTree(PersonRec*);
公众:
CTree();
//~CTree();
无效添加();
void视图();
};
//实现文件
#包括
#包括
使用名称空间std;
#包括“ctree.h”
CTree::CTree()
{
tree=NULL;
}
//个人列表::~MyTree()
//{
//
//}
bool CTree::IsEmpty()
{
if(tree==NULL)
{
返回true;
}
其他的
{
返回false;
}
}
void CTree::Add()
{
PersonRec*newPerson=newpersonrec();
姓名,20);
cout>新人->行贿;
newPerson->leftLink=NULL;
newPerson->rightLink=NULL;
AddItem(tree,newPerson);
}
void CTree::View()
{
if(IsEmpty())
{
coutbribe)
附加项(ptr->leftLink,newPer);
其他的
附加项(ptr->rightLink,newPer);
}
void CTree::DisplayTree(PersonRec*ptr)
{
如果(ptr==NULL)
返回;
显示树(ptr->rightLink);

CTree::AddItem
中,您的情况是错误的:

    if (tree == NULL)
    {
        ptr = newPer;
    }
应该是

    if (ptr == NULL)
    {
        ptr = newPer;
    }

当您调用
AddItem(ptr->rightLink,newPer);
时,
newPer
是新节点,但当前根的任何一个后代都可以是
NULL
,因此您需要检查并替换
NULL
,而不是新节点。

谢谢,我真不敢相信我没有捕捉到它。我花了几个小时研究这个:)ty