鱼C论坛

 找回密码
 立即注册
查看: 1606|回复: 4

[已解决]单链表课程 ,为什么没有为library申请内存却要释放它,申请了的却不释放

[复制链接]
发表于 2019-10-4 20:12:04 | 显示全部楼层 |阅读模式

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

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

x
#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;
}
最佳答案
2019-10-6 21:21:30
library是链表头指针。在addBook函数里,将每个book加入到链表中,在releaseLibrary函数中用循环将链表中所有book都释放掉了。

另外,releaseLibrary这个函数有点问题,先free(library),再访问library->next有问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-4 21:10:05 | 显示全部楼层
可以说清楚点吗  把具体代码 几行给说一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-5 10:34:54 | 显示全部楼层
int main(void)
{
        struct Book *library = NULL;       library 是一个Book类型的指针变量,没有分配内存。
        int ch;


void addBook(struct Book **library)
{
        struct Book *book, *temp;

        book = (struct Book *)malloc(sizeof(struct Book));        book 分布了内存空间
        if (book == NULL)
        {
                printf("内存分布失败\n");
                exit(1);
        }


void releaseLibrary(struct Book *library)
{
        while (library != NULL)
        {
                free(library);                      在结束程序时却把library内存释放了,library好像没有分配内存。
                library = library->next;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-6 21:21:30 | 显示全部楼层    本楼为最佳答案   
library是链表头指针。在addBook函数里,将每个book加入到链表中,在releaseLibrary函数中用循环将链表中所有book都释放掉了。

另外,releaseLibrary这个函数有点问题,先free(library),再访问library->next有问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-7 15:57:09 | 显示全部楼层
建议再在releaseLibrary这个函数里引入第二个指针来处理释放问题,一个指针做不到的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 02:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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