鱼C论坛

 找回密码
 立即注册
查看: 435|回复: 1

[已解决]求助

[复制链接]
发表于 2023-12-25 20:08:25 | 显示全部楼层 |阅读模式

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

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

x
为什么写入两个节点,第一个数据不见了


#include <stdio.h>
#include <stdlib.h>

struct Book
{
        char title[128];
        char author[40];
        struct Book* next;
};
void printBook (struct Book* book)
{
        do
        {
                printf("%s\n",book->title);
                printf("%s\n",book->author);
                book = book->next;
        }
        while(book->next!=NULL);
}
void getinput (struct Book** book)
{
        struct Book* new_book,*temp;
        new_book = (struct Book *)malloc(sizeof(struct Book));
        scanf("%s%s",new_book->title,new_book->author);
        if (*book == NULL)
        {
                *book = new_book;
                new_book->next = NULL;
        }
        else
        {
                temp = *book;
                *book = new_book;
                new_book->next = temp;
        }
}
void realse_book(struct Book* book)
{
        struct Book * nextone;
        do
        {
                nextone = book->next;
                free(book);
                book = nextone;
        }
        while(book->next!=NULL);
}
int main()
{
        int ch = 1;
        struct Book *book = NULL,*library = NULL;
        while (1)
        {
                printf("请输入要插入的书名和作者");
                getinput(&library);
                printf("是否退出?(1/0)");
                scanf("%d",&ch);
                if (ch ==0)
                {
                        break;
                }
        }
        printBook(library);
        return 0;
}

最佳答案
2023-12-25 20:08:43
问题出在getinput函数中,当book不为空时,你将新节点插入到了链表的头部,但是并没有把原始的链表连接起来。所以只有第一个输入的数据能够正常输出,后续的数据都无法访问。

解决方法是,在将新节点插入到链表头部之后,需要把原始的链表连接到新节点的next上,使得链表保持完整。

以下是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Book
  4. {
  5.     char title[100];
  6.     char author[100];
  7.     struct Book* next;
  8. };
  9. void printBook(struct Book* book)
  10. {
  11.     while (book != NULL) {
  12.         printf("%s\n", book->title);
  13.         printf("%s\n", book->author);
  14.         book = book->next;
  15.     }
  16. }
  17. void getinput(struct Book** book)
  18. {
  19.     struct Book* new_book = (struct Book*)malloc(sizeof(struct Book));
  20.     scanf("%s%s", new_book->title, new_book->author);
  21.     new_book->next = *book;  // 将原始链表连接到新节点的next上
  22.     *book = new_book;  // 更新链表头指针
  23. }
  24. void release_book(struct Book* book)
  25. {
  26.     struct Book* nextone;
  27.     while (book != NULL) {
  28.         nextone = book->next;
  29.         free(book);
  30.         book = nextone;
  31.     }
  32. }
  33. int main()
  34. {
  35.     int ch = 1;
  36.     struct Book* library = NULL;
  37.     while (ch == 1) {
  38.         printf("请输入要插入的书名和作者: ");
  39.         getinput(&library);
  40.         printf("是否退出?(1/0)");
  41.         scanf("%d", &ch);
  42.     }
  43.     printBook(library);
  44.     release_book(library);
  45.     return 0;
  46. }
复制代码

这样,即使插入多个节点,也能够正确输出所有的数据。同时,使用完链表后记得释放内存。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
屏幕截图 2023-12-25 200801.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-25 20:08:43 | 显示全部楼层    本楼为最佳答案   
问题出在getinput函数中,当book不为空时,你将新节点插入到了链表的头部,但是并没有把原始的链表连接起来。所以只有第一个输入的数据能够正常输出,后续的数据都无法访问。

解决方法是,在将新节点插入到链表头部之后,需要把原始的链表连接到新节点的next上,使得链表保持完整。

以下是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Book
  4. {
  5.     char title[100];
  6.     char author[100];
  7.     struct Book* next;
  8. };
  9. void printBook(struct Book* book)
  10. {
  11.     while (book != NULL) {
  12.         printf("%s\n", book->title);
  13.         printf("%s\n", book->author);
  14.         book = book->next;
  15.     }
  16. }
  17. void getinput(struct Book** book)
  18. {
  19.     struct Book* new_book = (struct Book*)malloc(sizeof(struct Book));
  20.     scanf("%s%s", new_book->title, new_book->author);
  21.     new_book->next = *book;  // 将原始链表连接到新节点的next上
  22.     *book = new_book;  // 更新链表头指针
  23. }
  24. void release_book(struct Book* book)
  25. {
  26.     struct Book* nextone;
  27.     while (book != NULL) {
  28.         nextone = book->next;
  29.         free(book);
  30.         book = nextone;
  31.     }
  32. }
  33. int main()
  34. {
  35.     int ch = 1;
  36.     struct Book* library = NULL;
  37.     while (ch == 1) {
  38.         printf("请输入要插入的书名和作者: ");
  39.         getinput(&library);
  40.         printf("是否退出?(1/0)");
  41.         scanf("%d", &ch);
  42.     }
  43.     printBook(library);
  44.     release_book(library);
  45.     return 0;
  46. }
复制代码

这样,即使插入多个节点,也能够正确输出所有的数据。同时,使用完链表后记得释放内存。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-8 12:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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