鱼C论坛

 找回密码
 立即注册
查看: 2259|回复: 2

[已解决]指针那点事

[复制链接]
发表于 2022-4-20 23:51:54 | 显示全部楼层 |阅读模式

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

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

x
我把小甲鱼的代码按我的理解改了一下,运行结果相同,学长瞄下我理解是否有误
这是我的代码
#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", 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("内存分配失败");
                exit(1);
        }
       
        getinput(book);
       
        if(*library != NULL)
        {
                book -> next = *library;  //直接把头指针指的地址给新节点
                *library = book;         //新节点的地址给头指针
        }
        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", book -> author);
                book = book -> next;
                count++;
        }
}

void releaselibrary(struct Book *library)
{
        struct Book *temp;
        while(library != NULL)
        {
                temp = library;
                library = library -> next;
                free(temp);
        }
}

int main()
{
        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;
                
}
这是小甲鱼的代码
#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", 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("内存分配失败");
                exit(1);
        }
       
        getinput(book);
       
        if(*library != NULL)
        {
                temp = *library;       //temp 中间变量保存头指针
                *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", book -> author);
                book = book -> next;
                count++;
        }
}

void releaselibrary(struct Book *library)
{
        struct Book *temp;
        while(library != NULL)
        {
                temp = library;
                library = library -> next;
                free(temp);
        }
}

int main()
{
        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;
                
}
最佳答案
2022-4-21 08:44:56
本帖最后由 jhq999 于 2022-4-21 08:52 编辑

头插法下面那个没必要有中间值,中间值一般用于互换
比如:
tmp=p;
p=p->next;
tmp->next=p->next;
p->next=tmp;
微信截图_20220420234431.png
微信截图_20220420234748.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-21 08:44:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-4-21 08:52 编辑

头插法下面那个没必要有中间值,中间值一般用于互换
比如:
tmp=p;
p=p->next;
tmp->next=p->next;
p->next=tmp;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-21 15:02:34 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 20:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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