带你学C带你飞得S1E45单链表中函数releaseLibrary存在什么错误?
代码如下:带你学C带你飞得S1E45单链表中函数releaseLibrary(struct Book *library)存在什么错误?#include <stdio.h>
#include <stdlib.h>
struct Book
{
char title;
char author;
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: \n", count);
printf("书名: %s\n", book->title);
printf("作者: %s\n\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);//这里和addBook(**library)是一样的么?
}
else
{
break;
}
}
printf("\n\n请问是否需要打印图书信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printLibrary(library);
}
releaseLibrary(library);
return 0;
}菜鸟先飞 void releaseLibrary(struct Book *library)
{
while (library != NULL)
{
free(library);
library = library->next;
}
} json 发表于 2017-6-18 14:22
看到S1E46视频时小甲鱼在视频中纠正了
void releaseLibrary(struct Book **library);
void releaseLibrary(struct Book **library)
{
struct Book *temp;
if(*library!=NULL)
{
temp=*library;
*library=(*library)->next;
free(temp);
}
} json 发表于 2017-6-18 14:22
好像没改正额
页:
[1]