|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么删除节点(不考虑头节点)会报错return value 3221225477
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct Book
{
char title[128];
char author[40];
struct Book* next;
};
void printBook (struct Book* book)
{
do
{
printf("%s\n",book->title);
printf("%s\n",book->author);
book = book->next;
}
while(book!=NULL);
}
void getinput (struct Book** book)
{
struct Book* new_book,*temp;
new_book = (struct Book *)malloc(sizeof(struct Book));
scanf("%s%s",new_book->title,new_book->author);
if (*book == NULL)
{
*book = new_book;
new_book->next = NULL;
}
else
{
temp = *book;
*book = new_book;
new_book->next = temp;
}
}
struct Book* delete_book(struct Book**book,char dele[128])
{
struct Book*tou = *book;
struct Book **temp = NULL;
do
{
temp = &((*book)->next);
if (strcmp((*temp)->title , dele) == 0){
temp = &((*temp)->next);
(*book)->next = *temp;
}
*book = (*book)->next;
}
while(*book!=NULL);
return tou;
}
void release_book(struct Book* book)
{
struct Book* nextone;
while (book != NULL) {
nextone = book->next;
free(book);
book = nextone;
}
}
int main()
{
int ch = 1,sh = 1;
struct Book* library = NULL;
char dele[100];
while (ch == 1) {
printf("请输入要插入的书名和作者: ");
getinput(&library);
printf("是否退出?(1/0)");
scanf("%d", &ch);
}
printf("是否删除图书(1/0)");
while (1){
scanf("%d",&sh);
if (sh == 0){
break;
}
printf("请输入要删除的书名");
scanf("%s",&dele);
library = delete_book(&library,dele);
printf("是否删除图书(1/0)");
}
printBook(library);
release_book(library);
return 0;
} |
|