좋아, 나는이 문제로 지쳐있다. 그래서 나는 도움이 될 것이라고 생각했다. 이 프로그램에는 직원 및 학생을 포함한 Personnel의 "데이터베이스"가 있습니다. 각 학생은 사용자가 삽입하고 검색 할 수있는 "서적"의 이진 트리를 가지고 있습니다. 나는 학생의 이름을 받아 특정 학생에게 해당하는 Personnel 노드를 찾아 그 학생의 bookTree에 책을 추가해야한다.바이너리 검색 트리/링크 된 목록에 노드를 삽입 하시겠습니까?
내가 갖는 오류 메시지가 "가 0xc0000005 : Homework4.exe에서 0x013c53a0에서 처리되지 않은 예외입니다. 액세스 위반 읽기 위치 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);
}
감사합니다! 그것은 참으로 문제입니다. 그러나 나는 아직도 더 많은 도움이 필요하다고 생각합니다. * root의 -> 키에 어떻게 액세스합니까? 또는 * root의 왼쪽 및 오른쪽 노드로 함수를 재귀 적으로 호출합니까? 나는 마치 * root-> key와 * (root-> right) 인 것처럼 느껴진다. 그러나 그것은 작동하지 않는다. 마찬가지로, 512 행의 book_traverse() 호출이 어떻게 이와 함께 변경됩니까? – Vance
(* root) -> key 및 (* root) -> right가 작동해야합니다. 그리고 아마도 book_traverse (student-> bookTree, & newBook) –