鱼C论坛

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

[已解决]建立动态链表,程序有问题

[复制链接]
发表于 2024-9-15 21:17:48 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)

struct Student
{
        long num;
        float score;
        struct Student *next;
};
int n;
struct Student*creat()
{
        struct Student*head, *p1, *p2;
        n = 0;
        p2 = p1 = (struct Student*)malloc(LEN);
       
        printf("输入学号和成绩\n");
        scanf_s("%ld,%f",&p1->num,&p1->score);
        head = NULL;
        while (p1->num)
        {
                n++;
                if (n == 1) head = p1;
                else        p2->next = p1;
                p2 = p1;
                p1 = (struct Student*)malloc(LEN);
                scanf_s("%ld,%f", &p1->num, &p1->score);
        }
        p2->next = NULL;
        return head;
}

int main()
{
        struct Student*p;
        p = creat();
        printf("\nnumber:%ld\nscore:%5.1f\n",p->num,p->score);
        while (1);
        return 0;
}
最佳答案
2024-9-15 21:17:59
你好,看了一下你给的代码,有一些地方需要注意和修改:

1. **`#include`部分**:需要包含具体的头文件。
2. **`scanf_s`用法问题**:`scanf_s`是微软特有的安全版本,使用时需要特别小心。
3. **输出问题**:最后输出只打印了第一个学生的信息。

下面是修改后的代码,保证更好的规范性和运行效果:

  1. #include <stdio.h>
  2. #include <stdlib.h>  // 包含 malloc、free 等函数
  3. #define LEN sizeof(struct Student)

  4. struct Student {
  5.     long num;
  6.     float score;
  7.     struct Student *next;
  8. };

  9. int n;

  10. struct Student* creat() {
  11.     struct Student *head, *p1, *p2;
  12.     n = 0;
  13.    
  14.     p2 = p1 = (struct Student*)malloc(LEN);
  15.    
  16.     printf("输入学号和成绩 (以0结束输入):\n");
  17.     scanf("%ld %f", &p1->num, &p1->score);  // 这里直接用scanf,简单处理
  18.    
  19.     head = NULL;
  20.     while (p1->num) {
  21.         n++;
  22.         if (n == 1)
  23.             head = p1;
  24.         else
  25.             p2->next = p1;
  26.         p2 = p1;
  27.         
  28.         p1 = (struct Student*)malloc(LEN);
  29.         scanf("%ld %f", &p1->num, &p1->score);
  30.     }
  31.     p2->next = NULL;  // 链表尾部指向NULL
  32.     free(p1);  // 释放最后多分配的一个节点内存
  33.     return head;
  34. }

  35. void print_list(struct Student *head) {
  36.     struct Student *p = head;
  37.     while (p != NULL) {
  38.         printf("number: %ld\nscore: %.1f\n", p->num, p->score);
  39.         p = p->next;
  40.     }
  41. }

  42. int main() {
  43.     struct Student *p;
  44.     p = creat();
  45.     if (p == NULL) {
  46.         printf("没有输入有效数据。\n");
  47.     } else {
  48.         print_list(p);
  49.     }
  50.    
  51.     // 这里可以加入代码释放链表内存
  52.     while (1);  // 暂停程序查看输出
  53.     return 0;
  54. }
复制代码


**关键修改点**:
1. `#include <stdio.h>` 和 `#include <stdlib.h>` 是包含标准输入输出和内存管理的头文件。
2. 将 `scanf_s` 改为 `scanf`。
3. 添加了 `print_list` 函数,以遍历并打印整个链表。
4. 在 `creat` 函数中释放了最后多分配的一个节点内存。
5. 如果链表为空打印提示信息。

这段改良后的代码结构清晰,也照顾到内存管理的基本问题。希望对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-9-15 21:17:59 | 显示全部楼层    本楼为最佳答案   
你好,看了一下你给的代码,有一些地方需要注意和修改:

1. **`#include`部分**:需要包含具体的头文件。
2. **`scanf_s`用法问题**:`scanf_s`是微软特有的安全版本,使用时需要特别小心。
3. **输出问题**:最后输出只打印了第一个学生的信息。

下面是修改后的代码,保证更好的规范性和运行效果:

  1. #include <stdio.h>
  2. #include <stdlib.h>  // 包含 malloc、free 等函数
  3. #define LEN sizeof(struct Student)

  4. struct Student {
  5.     long num;
  6.     float score;
  7.     struct Student *next;
  8. };

  9. int n;

  10. struct Student* creat() {
  11.     struct Student *head, *p1, *p2;
  12.     n = 0;
  13.    
  14.     p2 = p1 = (struct Student*)malloc(LEN);
  15.    
  16.     printf("输入学号和成绩 (以0结束输入):\n");
  17.     scanf("%ld %f", &p1->num, &p1->score);  // 这里直接用scanf,简单处理
  18.    
  19.     head = NULL;
  20.     while (p1->num) {
  21.         n++;
  22.         if (n == 1)
  23.             head = p1;
  24.         else
  25.             p2->next = p1;
  26.         p2 = p1;
  27.         
  28.         p1 = (struct Student*)malloc(LEN);
  29.         scanf("%ld %f", &p1->num, &p1->score);
  30.     }
  31.     p2->next = NULL;  // 链表尾部指向NULL
  32.     free(p1);  // 释放最后多分配的一个节点内存
  33.     return head;
  34. }

  35. void print_list(struct Student *head) {
  36.     struct Student *p = head;
  37.     while (p != NULL) {
  38.         printf("number: %ld\nscore: %.1f\n", p->num, p->score);
  39.         p = p->next;
  40.     }
  41. }

  42. int main() {
  43.     struct Student *p;
  44.     p = creat();
  45.     if (p == NULL) {
  46.         printf("没有输入有效数据。\n");
  47.     } else {
  48.         print_list(p);
  49.     }
  50.    
  51.     // 这里可以加入代码释放链表内存
  52.     while (1);  // 暂停程序查看输出
  53.     return 0;
  54. }
复制代码


**关键修改点**:
1. `#include <stdio.h>` 和 `#include <stdlib.h>` 是包含标准输入输出和内存管理的头文件。
2. 将 `scanf_s` 改为 `scanf`。
3. 添加了 `print_list` 函数,以遍历并打印整个链表。
4. 在 `creat` 函数中释放了最后多分配的一个节点内存。
5. 如果链表为空打印提示信息。

这段改良后的代码结构清晰,也照顾到内存管理的基本问题。希望对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 07:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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