#include <stdio.h>
#include <stdlib.h>
struct Book
{
char title[128];
char author[40];
struct Book *next;
};
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf_s("%s", book->title,127);
printf("请输入作者:");
scanf_s("%s", book->author,39);
}
void addBook(struct Book **library) //main函数中把library的地址传过来,但这里函数的参数定义是指针,二级指针
{
struct Book *book, *temp; //这里创造两个指向结构体book的指针
book = (struct Book *)malloc(sizeof(struct Book));
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", book->title);
printf("作者:%s", book->author);
book = book->next; count++;
}
}
void releaseLibrary(struct Book *library)
{
while (library != NULL)
{
library = library->next; free(library);
}
}
int main()
{
struct Book *library = NULL;
addBook(&library);
printLibrary(library);
}
小甲鱼视频讲的单链表头插法例子的问题
猜你喜欢
转载自blog.csdn.net/qq_23304241/article/details/88049353
今日推荐
周排行