【发布时间】:2021-08-22 15:52:26
【问题描述】:
我真的无法应付的家伙。我该怎么解决这个问题。我不断收到我的 root 错误,它总是提到 root 是 nullptr。然后,突然间,我所有的书籍记录都消失了/删除。我不知道如何解决这个问题,我一直在使用 strcmp 进行更改,但没有它,但仍然如此。如果我找到搜索过的内容也是如此,它会删除我所有的书籍记录。也许你们可以为我提供一些需要修复的错误语法,并通过编码解释我的错误。 This is the snapshot of the error
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
char title[50], author[50], ISBN[50], pubDate[20], searchAuthor[50];
struct node* left, * right;
};
struct node* delete_node(struct node* root, char deleteAuthor[]);
struct node* insert_node(struct node* root, char author[], char title[], char ISBN[], char pubDate[]);
struct node* FindMin(struct node*);
//struct node* search(struct node* root, char deleteAuthor[]);
void in_display(struct node* root);
void search(struct node* root, char searchAuthor[]);
void main()
{
struct node* root = NULL;
int ch, loop;
char title[50], author[50], ISBN[50], pubDate[20], searchAuthor[50], deleteAuthor[50];
printf("\n\n\t\t\tBinary Search Tree");
printf("\n\t\t-- To stores a library of book records");
printf("\n\t\t-- To search a library of book records");
printf("\n\t\t-- To delete a library of book records");
printf("\n\t\t-- To display a library of book records");
printf("\n\t=========================================================\n");
printf("\n\tState the number of book you need to input: ");
scanf("%d", &loop);
for (int c = 0; c < loop; c++) {
printf("\n\n\t\t\t--------Insert New Book--------");
printf("\n\tEnter the author of the book: \t\t\t");
scanf("\n%[^\n]%*c", author);
printf("\tEnter the title of the book: \t\t\t");
scanf("\n%[^\n]%*c", title);
printf("\tKey in the ISBN code: \t\t\t\t");
scanf("\n%[^\n]%*c", ISBN);
printf("\tEnter the Publication Date of the book: \t");
scanf("\n%[^\n]%*c", pubDate);
root = insert_node(root, author, title, ISBN, pubDate);
}
printf("\t\t\t--------Insert New Book End--------\n\n");
printf("\n\tPlease enter choice (1/2/3/4/5) based on the ....");
do
{
//printf("\n\t1.\tInsert a new Boook Record");
printf("\n\t1.\tDisplay all the Books record");
printf("\n\t2.\tDelete a Book Record");
printf("\n\t3.\tSearch a abook Record ");
printf("\n\t4.\tExit");
printf("\n\n\tEnter your choice: ");
scanf("%d", &ch);
switch (ch){
case 1:
printf("\n\t\t-----------------Library of Book Records-----------------\n");
in_display(root);
break;
case 2:
printf("\nEnter the author of the book to be deleted: \t");
scanf("\n%[^\n]%*c", deleteAuthor);
root = delete_node(root, deleteAuthor);
break;
case 3:
printf("\nEnter the author of the book to be searched: \t");
scanf("\n%[^\n]%*c", searchAuthor);
search(root, searchAuthor);
break;
case 4:
exit(0);
break;
default: printf("\n\tWrong Option\n");
}
} while (1);
}
struct node* insert_node(struct node* root, char author[], char title[], char ISBN[], char pubDate[])
{
if (root == NULL)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
strcpy(temp->author, author);
strcpy(temp->title, title);
strcpy(temp->ISBN, ISBN);
strcpy(temp->pubDate, pubDate);
temp->left = NULL;
temp->right = NULL;
return temp;
}
if (strcmp(author, root->author)<=0)
{
root->left = insert_node(root->left, author, title, ISBN, pubDate);
}
else
{
root->right = insert_node(root->right, author, title, ISBN, pubDate);
}
return root;
}
void in_display(struct node* root) {
if (root == NULL) {
return;
}
in_display(root->left);
printf("\n\tAuthor Name \t\t- %s", root->author);
printf("\n\tBook Title \t\t- %s", root->title);
printf("\n\tISBN \t\t\t- %s", root->ISBN);
printf("\n\tPublication Date \t- %s\n", root->pubDate);
in_display(root->right);
}
struct node* delete_node(struct node* root, char deleteAuthor[]) {
if (root == NULL) {
return root;
}
else if (strcmp(deleteAuthor, root->author) < 0) {
root->left = delete_node(root->left, deleteAuthor);
printf("\nBook Record Not Found\n");
}
else if (strcmp(deleteAuthor, root->author) > 0) {
root->right = delete_node(root->right, deleteAuthor);
printf("\nBook Record Not Found\n");
}
else {
if (root->left == NULL && root->right == NULL) {
free(root);
root = NULL;
printf("\nBook Record Successfully Deleted\n");
}
else if (root->left == NULL) {
struct node* temp = root;
root = root->right;
free(temp);
temp = NULL;
printf("\nBook Record Successfully Deleted\n");
}
else if (root->right == NULL) {
struct node* temp = root;
root = root->left;
free(temp);
temp = NULL;
printf("\nBook Record Successfully Deleted\n");
}
else {
struct node* temp = root;
root->left = FindMin(root);
root->left->right = root->right;
root = root->left;
strcpy(temp->author, root->author);
strcpy(temp->title, root->title);
strcpy(temp->ISBN, root->ISBN);
strcpy(temp->pubDate, root->pubDate);
free(temp);
temp = NULL;
printf("\nBook Record Successfully Deleted\n");
}
return root;
}
return root;
}
struct node* FindMin(struct node* root) {
while (root->left != NULL) {
root = root->left;
}
return root;
}
void search(struct node* root, char searchAuthor[]){
if (root->author < searchAuthor) {
return search(root->right, searchAuthor);
}
else if (root->author > searchAuthor) {
return search(root->left, searchAuthor);
}
else {
printf("\n\t\t\t--------Searched Book Found--------\n");
printf("\n\t\t\tAuthor Name \t\t- %s", root->author);
printf("\n\t\t\tBook Title \t\t- %s", root->title);
printf("\n\t\t\tISBN \t\t\t- %s", root->ISBN);
printf("\n\t\t\tPublication Date \t- %s\n", root->pubDate);
//return root;
}
}
【问题讨论】:
-
您发布了相同的问题,只有细微差别here。您的 IDE 屏幕截图中的代码与您在此处发布的代码不匹配。
-
我猜您当前遇到的错误是您没有检查指针是否为空。
search不返回任何东西?delete需要工作;什么是删除返回?我建议将警告提高到更高的级别。 -
@MOehm 好的,我已经更改了屏幕截图,但在搜索时仍然有问题
-
@Neil 我已经重新编辑了问题的快照。搜索功能出了点问题
-
当
search被声明为void时,你怎么能拥有return search(...?
标签: c struct linked-list binary-search-tree singly-linked-list