马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//鱼C论坛——BaysideLizard写于2023年11月30日
struct Book
{
char title[128];
char author[40];
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[128];
while(1)
{
printf("请问是否需要录入书籍信息[Y/n]:");
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("请问是否需要输出书籍信息[Y/n]:");
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("请问是否需要重新查找[y/N]:");
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/n]:y
请输入书名:《带你学C带你飞》
请输入作者:小甲鱼
请问是否需要录入书籍信息[Y/n]:
请输入书名:《零基础入门学习Scratch》
请输入作者:不二如是
请问是否需要录入书籍信息[Y/n]:Y
请输入书名:《零基础入门学习Python》
请输入作者:小甲鱼
请问是否需要录入书籍信息[Y/n]:n
请问是否需要输出书籍信息[Y/n]:
Book1:
书名:《带你学C带你飞》
作者:小甲鱼
Book2:
书名:《零基础入门学习Scratch》
作者:不二如是
Book3:
书名:《零基础入门学习Python》
作者:小甲鱼
请输入要搜索的书名或作者:大甲鱼
很抱歉,没能找到:(
请问是否需要重新查找[y/N]:y
请输入要搜索的书名或作者:小鱼
很抱歉,没能找到:(
请问是否需要重新查找[y/N]:Y
请输入要搜索的书名或作者:小甲鱼
已找到相关图书:
书名:《带你学C带你飞》
作者:小甲鱼
已找到相关图书:
书名:《零基础入门学习Python》
作者:小甲鱼
Process returned 0 (0x0) execution time : 74.824 s
Press any key to continue.
在FishC学C的第25天 |