马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
struct Book
{
char title[128];
char author[40];
struct Book*next;
};
void getInput(struct Book*book);
void addBook(struct Book**library);
void printLibrary(struct Book*library);
void releaseLibriary(struct Book**library);
void getInput(struct Book*book)
{
printf("请输入书名:\n");
scanf("%s",book->title);
printf("请输入作者:\n");
scanf("%s",book->author);
}
void addBook(struct Book**library)
{
struct Book*book,*temp;
book=(struct Book*)malloc(sizeof(struct Book));
if(book==NULL)
{
printf("分配内存失败。\n");
exit(1);
}
getInput(book);
if(*library!=NULL)
{
temp=*library;
*library=book;
book->next=*library;
}
else
{
*library=book;
book=NULL;
}
}
void printLibrary(struct Book*library)
{
struct Book*book;
book=library;
int count=1;
while(book!=NULL)
{
printf("Book%d",count);
printf("书名:%s",book->title);
printf("作者:%s",book->author);
book=book->next;
count++;
}
}
void releaseLibriary(struct Book**library)
{
struct Book*temp;
while(*library!=NULL)
{
temp=*library;
*library=(*library)->next;
free(temp);
}
}
int main(void)
{
struct Book*library=NULL;
int ch;
while(1)
{
printf("是否需要输入信息(Y/N):\n");
do
{
ch=getchar();
}while(ch!='Y'&ch!='N');
if(ch=='Y')
{
addBook(&library);
}
else
{
break;
}
printf("是否需要打印:\n");
do
{
ch=getchar();
}while(ch!='Y'&ch!='N');
if(ch=='Y')
{
printLibrary(library);
}
else
{
break;
}
releaseLibrary(&library);
}
return 0;
}
|