鱼C论坛

 找回密码
 立即注册
查看: 674|回复: 5

[已解决]关于单链表的问题

[复制链接]
发表于 2020-8-17 15:13:43 | 显示全部楼层 |阅读模式

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

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

x
为什么这个while循环只执行了一次就退出了?
源代码:
#include <stdio.h>
#include <stdlib.h>
#define Max 128

struct Stu
{
        int ID;
        int score;
        char name[Max];
        struct Stu* next;
};
void input(struct Stu* stu);
void input(struct Stu* stu)
{
        printf("请输入学生的编号:");
        scanf("%d",stu->ID);
        getchar();
        printf("请输入学生的姓名:");
        scanf("%s",stu->name);
        getchar();
        printf("请输入学生的成绩:");
        scanf("%d",stu->score);
}
void Add(struct Stu** school);
void Add(struct Stu** school)
{
        struct Stu* stu,*temp;
        stu = (struct Stu*)malloc(sizeof(struct Stu));
        if(stu == NULL)
        {
                fputs("内存分配失败!\n",stderr);
                exit(1);
        }
        input(stu);
        if(*school == NULL)
        {
                *school = stu;
                stu->next = NULL;
        }
        else
        {
                temp = *school;
                *school = stu;
                stu->next = temp;
        }
}
void print(struct Stu* head);
void print(struct Stu* head)
{
        struct Stu* stu;
        int count = 1;
        stu = head;
        while(stu != NULL)
        {
                printf("%d:学生的ID为:%d,学生的姓名为:%s,学生的分数为:%d\n",count,stu->ID,stu->name,stu->score);
                count++;
                stu = stu->next;
        }
}
void close(struct Stu** school);
void close(struct Stu** school)
{
        struct Stu* temp;
        while(*school != NULL)
        {
                temp = *school;
                *school = (*school)->next;
                free(temp);
        }
}
int main(void)
{
        struct Stu* head = NULL;
        int ch;
        while(1)
        {
                printf("是否录入数据(Y/N):");
                do
                {
                        ch = getchar();
                }while(ch != 'Y' && ch != 'N');
                if(ch == 'Y')
                {
                        Add(&head);
                }
                else
                {
                        break;
                }
                print(head);
                close(&head);
        }
        return 0;
}
最佳答案
2020-8-17 15:29:58
程序的问题在input函数中
学生编号和学生成绩都是int类型,但是scanf的时候,后面没有写&

应该是
scanf("%d",&stu->ID);
scanf("%d",&stu->score);

另外你在while(1)中调用了close函数,若多次输入数据,那也只能显示最后一次输入的数据,前面的都被你free释放掉了
2020-08-17.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-17 15:29:58 | 显示全部楼层    本楼为最佳答案   
程序的问题在input函数中
学生编号和学生成绩都是int类型,但是scanf的时候,后面没有写&

应该是
scanf("%d",&stu->ID);
scanf("%d",&stu->score);

另外你在while(1)中调用了close函数,若多次输入数据,那也只能显示最后一次输入的数据,前面的都被你free释放掉了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-17 15:30:33 | 显示全部楼层
scanf() 需要加上取址符

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define Max 128

  4. struct Stu
  5. {
  6.     int ID;
  7.     int score;
  8.     char name[Max];
  9.     struct Stu *next;
  10. };
  11. void input(struct Stu *stu);
  12. void input(struct Stu *stu)
  13. {
  14.     printf("请输入学生的编号:");
  15.     scanf("%d", &stu->ID);
  16.     getchar();
  17.     printf("请输入学生的姓名:");
  18.     scanf("%s", stu->name);
  19.     getchar();
  20.     printf("请输入学生的成绩:");
  21.     scanf("%d", &stu->score);
  22. }
  23. void Add(struct Stu **school);
  24. void Add(struct Stu **school)
  25. {
  26.     struct Stu *stu, *temp;
  27.     stu = (struct Stu *)malloc(sizeof(struct Stu));
  28.     if (stu == NULL)
  29.     {
  30.         fputs("内存分配失败!\n", stderr);
  31.         exit(1);
  32.     }
  33.     input(stu);
  34.     if (*school == NULL)
  35.     {
  36.         *school = stu;
  37.         stu->next = NULL;
  38.     }
  39.     else
  40.     {
  41.         temp = *school;
  42.         *school = stu;
  43.         stu->next = temp;
  44.     }
  45. }
  46. void print(struct Stu *head);
  47. void print(struct Stu *head)
  48. {
  49.     struct Stu *stu;
  50.     int count = 1;
  51.     stu = head;
  52.     while (stu != NULL)
  53.     {
  54.         printf("%d:学生的ID为:%d,学生的姓名为:%s,学生的分数为:%d\n", count, stu->ID, stu->name, stu->score);
  55.         count++;
  56.         stu = stu->next;
  57.     }
  58. }
  59. void close(struct Stu **school);
  60. void close(struct Stu **school)
  61. {
  62.     struct Stu *temp;
  63.     while (*school != NULL)
  64.     {
  65.         temp = *school;
  66.         *school = (*school)->next;
  67.         free(temp);
  68.     }
  69. }
  70. int main(void)
  71. {
  72.     struct Stu *head = NULL;
  73.     int ch;
  74.     while (1)
  75.     {
  76.         printf("是否录入数据(Y/N):");
  77.         do
  78.         {
  79.             ch = getchar();
  80.         } while (ch != 'Y' && ch != 'N');
  81.         if (ch == 'Y')
  82.         {
  83.             Add(&head);
  84.         }
  85.         else
  86.         {
  87.             break;
  88.         }
  89.         printf("1");
  90.         print(head);
  91.         close(&head);
  92.     }
  93.     return 0;
  94. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-17 21:12:41 | 显示全部楼层
sunrise085 发表于 2020-8-17 15:29
程序的问题在input函数中
学生编号和学生成绩都是int类型,但是scanf的时候,后面没有写&

谢谢,但是还是不明白取址符为什么会影响到while循环...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-17 21:15:52 | 显示全部楼层
zltzlt 发表于 2020-8-17 15:30
scanf() 需要加上取址符

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

使用道具 举报

 楼主| 发表于 2020-8-17 21:17:54 | 显示全部楼层
sunrise085 发表于 2020-8-17 15:29
程序的问题在input函数中
学生编号和学生成绩都是int类型,但是scanf的时候,后面没有写&

哈哈,我都没注意到我的print和close在while里面,写的时候太粗心了,对单链表这块知识理解的不是很好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 07:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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