将节点插入二进制搜索树/链表



好吧,我对这个问题已经筋疲力尽了,所以我想我应该得到一些外部帮助。该项目拥有一个人事"数据库",其中包括员工和学生。每个学生都有一个"书"的二叉树,用户可以插入并搜索。我需要取一个学生的名字,找到与该特定学生对应的人员节点,并将一本书添加到该学生的bookTree中。

我收到的错误消息是"Homework4.exe中0x013c53a0处未处理的异常:0xC0000005:读取位置0xcccccd1c的访问冲突。",我认为这意味着我在某个地方弄乱了指针。调用堆栈将第512行(以及book_traverse())显示为问题制造者。这就是我到目前为止所做的(省略了不必要的代码):提前谢谢!

class PersonnelNode {       // This is a container class
private:
    Personnel       *pNode; // It contains a Personnel class
    PersonnelNode   *pNext; // pointer used to form a linked list
public:
    void setNode(Personnel *pNode) { this->pNode = pNode; }
    void setNext(PersonnelNode *pNext) { this->pNext = pNext; }
    Personnel* getNode() { return pNode; }
    PersonnelNode* getNext() { return pNext; }
    PersonnelNode() {       // constructor
        pNode = NULL;
        pNext = NULL;
    }
} *head = NULL; // declare a global pointer variable head

struct Book {
  char title[75];
  char url[75];
  char key;
  Book *left;
  Book *right;  
  Book(char *title, char *url) { // Constructor
    strcpy_s(this->title, title);
    strcpy_s(this->url, url);
    key = title[0];
    left = NULL;
    right = NULL;
  }
};

class Student : public Personnel { //inherit from Personnel
   ... (omitted the unnecessary code)
   Book *bookTree;

   //BookTree = NULL in constructor
}

int insert_book() {
   PersonnelNode *temp, *prev;
   Personnel *person;
   Student *student;
   Book *newBook;
   char title[75], url[75], sName[75];
   temp = head;
   cout << endl << "@Inserting book node.........." << endl;
   cout << "Enter the student name: ";
   cin.ignore();
   cin.getline(sName, 75);
       //*****My error is probably below here?
   while (temp != NULL) {
       person = temp->getNode();
       if (sName != person->getName()) {
           prev = temp;
           temp = temp->getNext();
       }
       else {
           student = (Student *) person;
       }
   }
   cout << "Enter the book title: ";
   cin.getline(title, 75);
   cout << "Enter the URL: ";
   cin.getline(url, 75);
   newBook = new Book(title, url);
   book_traverse(student->bookTree, newBook); //LINE 512
   return 0;
}

//***Recursive function to insert book
void book_traverse(Book* root, Book* newBook) { //Is this right?
   if (root == NULL)                           //I tried Book* &root, but then
    root = newBook;                        //the compiler doesn't like root==NULL
   else if (newBook->key < root->key)
    book_traverse(root->left, newBook);
   else
    book_traverse(root->right, newBook);
}

我想你需要书**

void book_traverse(Book** root, Book* newBook) 

然后使用*root而不是处处使用root,例如

*root = newBook

否则,在book_traverse中,您将更改root的本地副本。

声明并初始化必要的变量

  1. 列出项目
  2. 读取要插入树中的数据项,比如x
  3. 创建一个新节点,使其左右指针为null
  4. 将数据x分配给新节点的信息字段
  5. 如果(树==NULL)则树=新节点的地址else if(x<tree->info)if(tree->left==NULL)然后树->left=新节点其他的tree=tree->left重复步骤5。else if(x>tree->info)if(tree->right==NULL)然后树->右=新节点其他的tree=tree->right重复步骤5else if(x==tree->info)打印"重复数据"并退出
  6. 对于下一次插入,转到步骤5

参考:http://www.programmers-point.blogspot.in

相关内容

  • 没有找到相关文章

最新更新