哪有把声明写在结构体定义前面的。。。#include <stdio.h>
#include <stdlib.h>
# include <malloc.h>
struct Book{
char title[128];
char author[40];
struct Book *next;
}*book,*temp;
void addBook(struct Book **library);
void getinput(struct Book *book);
void printfbook(struct Book *library);
void freelibrary(struct Book *book);
void getinput(struct Book *book){
printf("请输入书名[title]\n");
scanf("%s",book->title);
printf("请输入作者[author]\n");
scanf("%s",book->author);
}
void addBook(struct Book **library){
//struct Book *book,*temp;
book=NULL;
temp=NULL;
book=(struct Book*)malloc(sizeof(struct Book));
if(book==NULL){
printf("内存分配失败了");
exit(1);
}
else{
getinput(book);
}
if (*library != 0)
{
temp = *library;
*library = book;
book->next = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printfbook(struct Book *library){
// struct Book *book;
book=library;
int i=1;
while(book!=NULL){
printf("您录入的第%d本书的书名是:%s\n",i,book->title);
printf("您录入的第%d本书的作者是:%s\n",i,book->author);
i++;
book=book->next;
}
}
void freelibrary(struct Book *library){
//struct Book* temp;//temp跳出函数之后会自动释放,所以不用操心他的存在
while(library!=NULL){
temp=library->next;
free(library);
library=temp;
}
}
int main(){
struct Book *library=NULL;
addBook(&library);
char ans;
while(1){
printf("是否继续录入书本?Y/N\n");
scanf("%s",&ans);
if(ans=='Y'){
printf("请继续录入书本信息\n");
addBook(&library);
}
else{
break;
}
}
printf("是否打印目录?Y/N\n");
scanf("%s",&ans);
if(ans=='Y'){
printf("开始打印书目\n");
printfbook(library);
}
else{
printf("再见了您勒");
}
freelibrary(library);
return 0;
}
|