#include "stdio.h"
#include<stdlib.h>
#include<string.h>
struct book
{
char bookname[20];
char author[20];
struct book *next;
};
void input(struct book **library)
{
struct book *p;
static struct book q;
p=(struct book)malloc(sizeof(struct book));
if(p==NULL)
{
printf("内存分配错误");
}
printf("请输入书名:");
scanf("%s",p->bookname);
printf("请输入作者:");
scanf("%s",p->author);
if(*library==NULL)
{
*library=p;
p=p->next;
}
else
{
q->next=p;
p->next=NULL;
}
q=p;
}
int main()
{
struct book *library=NULL;
char i;
while(1)
{
printf("是否输入书籍?");
fflush(stdin);
i=getchar();
fflush(stdin);
if(i=='Y'||i=='y')
{
input(&library);
}
}
return 0;
}
void input(struct book **library)
{
struct book *p;
static struct book *q;
p=(struct book)malloc(sizeof(struct book));
if(p==NULL)
{
printf("内存分配错误");
}
p->next = NULL;
printf("请输入书名:");
scanf("%s",p->bookname);
printf("请输入作者:");
scanf("%s",p->author);
if(*library==NULL)
*library=p;
else
q->next=p;
q = p;
}