S1E46:单链表2--笔记
#include <stdio.h>#include <stdlib.h>
#include <string.h>
//鱼C论坛——BaysideLizard写于2023年11月30日
struct Book
{
char title;
char author;
struct Book *next;
//申请一个单链表节点
};
void addBook(struct Book **headLibrary);
void inputBook(struct Book *book);
void printLibrary(struct Book *headLibrary);
void releaseLibrary(struct Book *headLibrary);
struct Book *searchBook(struct Book *library,char *searchtarget);
void printsearchresult(struct Book *searchresult);
int main()
{
struct Book *headLibrary = NULL,*searchresult;
char ch,searchtarget;
while(1)
{
printf("请问是否需要录入书籍信息:");
do
{
ch = getchar();
if(ch != '\n')
{
getchar();
}
}while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'&&ch!='\n');
if(ch == 'y' || ch == 'Y' || ch == '\n')
{
addBook(&headLibrary);
}
else
{
break;
}
}
while(1)
{
printf("请问是否需要输出书籍信息:");
do
{
ch = getchar();
if(ch != '\n')
{
getchar();
}
}while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'&&ch!='\n');
if(ch == 'y' || ch == 'Y' || ch == '\n')
{
printLibrary(headLibrary);
break;
}
else
{
break;
}
}
searchbook:
printf("\n请输入要搜索的书名或作者:");
scanf("%s",searchtarget);
getchar();
searchresult = searchBook(headLibrary,searchtarget);
if(searchresult == NULL)
{
printf("很抱歉,没能找到:(\n");
printf("请问是否需要重新查找:");
while(1)
{
do
{
ch = getchar();
if(ch != '\n')
{
getchar();
}
}while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'&&ch!='\n');
if(ch == 'n' || ch == 'Y' || ch == '\n')
{
break;
}
else
{
goto searchbook;
}
}
}
else
{
do
{
printf("已找到相关图书:\n");
printsearchresult(searchresult);
}while(searchBook(searchresult->next,searchtarget) != NULL);
}
releaseLibrary(headLibrary);
return 0;
}
//插入一个单链表节点(尾插法)
void addBook(struct Book **headLibrary)
{
struct Book *book,*temp;
static struct Book *tail;
book = (struct Book *)malloc(sizeof(struct Book));
if(book == NULL)
{
printf("Memory allocation failed :(");
exit(1);
}
inputBook(book);
if(*headLibrary != NULL)
{
//插入数据
tail->next = book;
book->next = NULL;
}
else
{
*headLibrary = book;
book->next = NULL;
}
//修改指向单链表尾部的指针使之指向新的尾部
tail = book;
}
void inputBook(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
getchar();
printf("请输入作者:");
scanf("%s",book->author);
getchar();
}
void printLibrary(struct Book *headLibrary)
{
struct Book *book;
int count = 1;
book = headLibrary;
while(book != NULL)
{
printf("Book%d:\n",count);
printf("书名:%s\n",book->title);
printf("作者:%s\n",book->author);
book = book->next;
++count;
}
}
//依次释放单链表节点
void releaseLibrary(struct Book *headLibrary)
{
struct Book *temp;
while(headLibrary != NULL)
{
temp = headLibrary;
headLibrary = headLibrary->next;
free(temp);
}
}
//搜索单链表
struct Book *searchBook(struct Book *library,char *searchtarget)
{
struct Book *searchresult;
searchresult = library;
while(searchresult != NULL)
{
if(strcmp(searchresult->title,searchtarget) == 0 || strcmp(searchresult->author,searchtarget) == 0)
{
break;
}
searchresult = searchresult->next;
}
return searchresult;
}
//打印搜索结果
void printsearchresult(struct Book *searchresult)
{
printf("书名:%s\n",searchresult->title);
printf("作者:%s\n",searchresult->author);
}
运行结果:
请问是否需要录入书籍信息:y
请输入书名:《带你学C带你飞》
请输入作者:小甲鱼
请问是否需要录入书籍信息:
请输入书名:《零基础入门学习Scratch》
请输入作者:不二如是
请问是否需要录入书籍信息:Y
请输入书名:《零基础入门学习Python》
请输入作者:小甲鱼
请问是否需要录入书籍信息:n
请问是否需要输出书籍信息:
Book1:
书名:《带你学C带你飞》
作者:小甲鱼
Book2:
书名:《零基础入门学习Scratch》
作者:不二如是
Book3:
书名:《零基础入门学习Python》
作者:小甲鱼
请输入要搜索的书名或作者:大甲鱼
很抱歉,没能找到:(
请问是否需要重新查找:y
请输入要搜索的书名或作者:小鱼
很抱歉,没能找到:(
请问是否需要重新查找:Y
请输入要搜索的书名或作者:小甲鱼
已找到相关图书:
书名:《带你学C带你飞》
作者:小甲鱼
已找到相关图书:
书名:《零基础入门学习Python》
作者:小甲鱼
Process returned 0 (0x0) execution time : 74.824 s
Press any key to continue.
在FishC学C的第25天 这段代码是一个简单的图书管理系统,使用单链表实现。下面是对代码的解析和修改建议。
1. 在定义Book结构体时,title和author应该为字符串类型(char数组),而不是char类型。需要修改如下:
struct Book
{
char title;
char author;
struct Book *next;
};
2. inputBook函数中的scanf需要使用%s格式化输入字符串,因此要将book->title和book->author改为数组名,即book->title和book->author。修改如下:
void inputBook(struct Book *book)
{
printf("请输入书名:");
scanf("%s", book->title);
getchar();
printf("请输入作者:");
scanf("%s", book->author);
getchar();
}
3. 在searchBook函数中,searchresult初始值应该设置为NULL。修改如下:
struct Book *searchBook(struct Book *library, char *searchtarget)
{
struct Book *searchresult = NULL;
// ...
}
4. 在printsearchresult函数中,输出搜索结果前需要检查searchresult是否为空。修改如下:
void printsearchresult(struct Book *searchresult)
{
if (searchresult != NULL)
{
printf("书名:%s\n", searchresult->title);
printf("作者:%s\n", searchresult->author);
}
else
{
printf("很抱歉,没能找到:(\n");
}
}
5. 为了避免内存泄漏,releaseLibrary函数在释放每个节点之前,需要先将headLibrary指针指向下一个节点。修改如下:
void releaseLibrary(struct Book *headLibrary)
{
struct Book *temp;
while (headLibrary != NULL)
{
temp = headLibrary->next;
free(headLibrary);
headLibrary = temp;
}
}
以上是对代码的修改建议,希望能帮助到你。如果还有疑问,请随时追问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]