关于单链表打印图书的代码问题,求大神帮助!
#include<stdio.h>
#include<stdlib.h>
struct Book//声明一个单链表book的一个节点
{
char title;
char author;
struct Book *next;
};
void getInput(struct Book *book);
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
}
void addBook(struct Book **library);//参数为一个指向头指针的指针==头指针地址)
void addBook(struct Book **library)
{
struct Book *book;
struct Book *temp;//一个临时变量
book = (struct Book *)malloc(sizeof(struct Book));//给book指针申请一个动态内存
if(book == NULL)
{
printf("内存分配失败!");
exit(1);
}
getInput(book);//调用getInput函数传入书籍信息
if(*library != NULL) //对**library进行一层解引用,得到的是头指针,这里是头指针不指向NULL,也就是不为空链表的情况
{
temp = *library;
*library = book;
book->next = temp;
}
else//头指针指向NULL的情况 ,为空链表
{
*library = book;//将指向第一本书的指针赋值给头指针,使得头指针指向这本书
book->next = NULL;//book的下一个节点指向NULL
}
}
void printLibrary(struct Book *library);//形参为头指针
void printLibrary(struct Book *library)//形参为头指针
{
struct Book *book;
int count = 1;
book = library;
while(book != NULL)
{
printf("Book:%d", count);
printf("书名:%s", book->title);
printf("作者:%s", book->author);
book = book->next;//将下本书的地址传给book
count++;
}
}
void release(struct book **library);
void release(struct book **library)
{
struct Book *temp;
while(*library != NULL)
{
temp = *library;
*library = (*library)->next;
free(temp);
}
}
int main(void)
{
int ch;
struct Book *library = NULL;//头指针刚开始指向NULL,是一个空链表
while(1)
{
printf("请问是否需要录入书籍信息(Y/N)");
do
{
ch = getchar();
} while(ch != 'Y' && ch !='N');
if(ch == 'Y');
{
addBook(&library);
}
else
{
break;
}
}
printf("请问是否需要打印图书信息?(Y/N)");
do
{
ch = getchar();
} while(ch != 'Y' && ch !='N');
if(ch == 'Y');
{
printLibrary(library);
}
else
{
break;
}
release(&library);
return 0;
}
Dev运行的时候65行红了,请问一下怎么解决呢 本帖最后由 jackz007 于 2020-11-25 18:39 编辑
这 2 句
void release(struct book **library);
void release(struct book **library)
改为
void release(struct Book **library);
void release(struct Book **library)
这 1 句(90、105行)
if(ch == 'Y');
改为
if(ch == 'Y')
删除 109 ~ 113 行代码 jackz007 发表于 2020-11-25 18:27
这 2 句
改为
谢谢谢谢!好愚蠢的错误啊!!但是就是没有发现{:10_277:}
页:
[1]