鱼生富贵 2022-03-22 20:18 采纳率: 97.4%
浏览 103
已结题

关于#链表#的问题,如何解决?

利用链表记录输入的学生信息(学号、姓名、性别、年龄、得分、地址)。其中,学号长 度不超过 20, 姓名长度不超过 40, 性别长度为 1, 地址长度不超过 40

输入说明
包括若干行,每一行都是一个学生的信息,如: 00630018 zhouyan m 20 10.0 28#460 输入的最后以"end"结束
输出说明
将输入的内容倒序输出。每行一条记录,按照 学号 姓名 性别 年龄 得分 地址 的格式输出
我写的代码在VS上是对的,但是在平台提交零分,是哪里不合题意吗,还是输入方式太鸡肋了


#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
typedef struct Student
{
    char num[20];
    char name[40];
    char sex;
    int age;
    int score;
    char address[40];
    struct Student* next;
}student;
int main()
{
    char c = '\0';
    student* p1 = NULL, * p2 = NULL, * head = NULL, * L, * p;
    L = (student*)malloc(LEN); L->next = NULL;
    while (c != '\n')
    {
        p1 = (student*)malloc(LEN);
        scanf_s("%s", p1->num, 20);
        scanf_s("%s", p1->name, 40);
        c = getchar();
        scanf_s("%c", &p1->sex,1);
        scanf_s("%d", &p1->age);
        scanf_s("%d", &p1->score);
        scanf_s("%s", p1->address, 40);
        c = getchar();
        if (head == NULL)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
    }
    p1->next = NULL;
    L->next = head;
    p1 = p2 = L->next; p1 = p1->next;
    L->next = NULL;
    while (p1)
    {
        p2->next = L->next;
        L->next = p2;
        p2 = p1;
        p1 = p1->next;
    }
    p = L->next;
    while (p)
    {
        printf("%s ", p->num);
        printf("%s ", p->name);
        printf("%c ", p->sex);
        printf("%d ", p->age);
        printf("%d ", p->score);
        printf("%s ", p->address);
        printf("\n");
        p = p->next;
    }
    return 0;
}
}
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-03-22 22:13
    关注

    修改处见注释,供参考:

    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    #define LEN sizeof(struct Student)
    typedef struct Student
    {
        char  num[20];
        char  name[40];
        char  sex;
        int   age;
        float score;  //int score; //修改
        char  address[40];
        struct Student* next;
    }student;
    int main()
    {
        char num[20],name[40],sex,address[40];//修改
        int  age;     //修改
        float score;  //修改
        student* p1 = NULL, * p2 = NULL, * head = NULL, * L, * p;
        L = (student*)malloc(LEN); L->next = NULL;
        while (1)    //修改
        {
            scanf_s("%s", num, 20);
            if (strcmp(num,"end") == 0) break;
            scanf_s("%s", name, 40);
            getchar();
            scanf_s(" %c", &sex,1);
            scanf_s("%d", &age);
            scanf_s("%f", &score);
            getchar();
            scanf_s("%s", address, 40);
    
            p1 = (student*)malloc(LEN);
            strcpy(p1->num,num);strcpy(p1->name,name); //修改
            p1->sex = sex;      p1->age =age;          //修改
            p1->score = score;  strcpy(p1->address,address);//修改
    
            p1->next = L->next; //修改
            L->next = p1;       //修改
    
            //if (head == NULL)
            //    head = p1;
            //else
            //    p2->next = p1;
            //p2 = p1;
        }
        //p1->next = NULL;
        //L->next = head;
        //p1 = p2 = L->next; p1 = p1->next;
        //L->next = NULL;
        //while (p1)
        //{
        //    p2->next = L->next;
        //    L->next = p2;
        //    p2 = p1;
        //    p1 = p1->next;
        //}
        p = L->next;
        while (p)
        {
            printf("%s ", p->num);
            printf("%s ", p->name);
            printf("%c ", p->sex);
            printf("%d ", p->age);
            printf("%f ", p->score); //printf("%d ", p->score); //修改
            printf("%s ", p->address);
            printf("\n");
            p = p->next;
        }
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 修改了问题 3月22日
  • 创建了问题 3月22日

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型