鱼C论坛

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

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

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式

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

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

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;
}
最佳答案
6 天前
你好,看了一下你给的代码,有一些地方需要注意和修改:

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

下面是修改后的代码,保证更好的规范性和运行效果:
#include <stdio.h>
#include <stdlib.h>  // 包含 malloc、free 等函数
#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("输入学号和成绩 (以0结束输入):\n");
    scanf("%ld %f", &p1->num, &p1->score);  // 这里直接用scanf,简单处理
    
    head = NULL;
    while (p1->num) {
        n++;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f", &p1->num, &p1->score);
    }
    p2->next = NULL;  // 链表尾部指向NULL
    free(p1);  // 释放最后多分配的一个节点内存
    return head;
}

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

int main() {
    struct Student *p;
    p = creat();
    if (p == NULL) {
        printf("没有输入有效数据。\n");
    } else {
        print_list(p);
    }
    
    // 这里可以加入代码释放链表内存
    while (1);  // 暂停程序查看输出
    return 0;
}

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

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

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

使用道具 举报

发表于 6 天前 | 显示全部楼层    本楼为最佳答案   
你好,看了一下你给的代码,有一些地方需要注意和修改:

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

下面是修改后的代码,保证更好的规范性和运行效果:
#include <stdio.h>
#include <stdlib.h>  // 包含 malloc、free 等函数
#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("输入学号和成绩 (以0结束输入):\n");
    scanf("%ld %f", &p1->num, &p1->score);  // 这里直接用scanf,简单处理
    
    head = NULL;
    while (p1->num) {
        n++;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f", &p1->num, &p1->score);
    }
    p2->next = NULL;  // 链表尾部指向NULL
    free(p1);  // 释放最后多分配的一个节点内存
    return head;
}

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

int main() {
    struct Student *p;
    p = creat();
    if (p == NULL) {
        printf("没有输入有效数据。\n");
    } else {
        print_list(p);
    }
    
    // 这里可以加入代码释放链表内存
    while (1);  // 暂停程序查看输出
    return 0;
}

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

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 15:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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