马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
图片中的内容全部理解,但是代码中的library是什么意思,和图片中的变量a有什么关系#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 releaseLibrary(struct Book *library);
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
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 = temp;
}
else
{
*library = book;
book->next = NULL;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count = 1;
book = library;
while (book != NULL)
{
printf("Book%d: ", count);
printf("书名: %s\n", book->title);
printf("作者: %s\n", book->author);
book = book->next;
count++;
}
}
void releaseLibrary(struct Book *library)
{
while (library != NULL)
{
free(library);
library = library->next;
}
}
int main(void)
{
struct Book *library = NULL;
int ch;
while (1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
printf("请问是否需要打印图书信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printLibrary(library);
}
releaseLibrary(library);
return 0;
}
library 就是图书馆~
你每一次透过 addBook 增加一本书
temp = *library;
*library = book;
book->next = temp;
有一些差别在于你是头插法或尾插法跟插入指定位置而已,手法一样
|