鱼C论坛

 找回密码
 立即注册
查看: 2565|回复: 3

带你学C带你飞得S1E45单链表中函数releaseLibrary存在什么错误?

[复制链接]
发表于 2017-6-18 13:30:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:带你学C带你飞得S1E45单链表中函数releaseLibrary(struct Book *library)存在什么错误?

#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: \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;
}

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-18 14:22:07 | 显示全部楼层
  1. void releaseLibrary(struct Book *library)
  2. {
  3.         while (library != NULL)
  4.         {
  5.                 free(library);
  6.                 library = library->next;
  7.         }
  8. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-18 19:17:45 | 显示全部楼层

看到S1E46视频时小甲鱼在视频中纠正了
void releaseLibrary(struct Book **library);
void releaseLibrary(struct Book **library)
{
        struct Book *temp;
        if(*library!=NULL)
        {
                temp=*library;
               
                *library=(*library)->next;
                free(temp);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-6-19 20:23:23 | 显示全部楼层

好像没改正额
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 19:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表