bugustudy 2024-04-24 17:34 采纳率: 0%
浏览 5
已结题

c语言链表结构体数据插入


struct Date {
    int year;
    int month;
    int day;
};


struct Student {
    char NO[10];
    char name[50];
    struct Date birthday;
}stu[50];


struct Node {
    struct Student* one;
    struct Node* next;
};
struct Node* createList(struct Node** head, char courseName[40])//使用头插
{
    assert(head);
    printf("%s", courseName);
    printf("请输入学生学号\n");
    char no[10];
    int i = 0;
    while (scanf("%s", no) != EOF)
    {
        if (!strcmp("0", no))//输入学号为0结束输入
        {
            break;
        }
        else {
            for (i = 0; i <count; i++)//输入的学号与stu[i]里的学号匹配
            {
                if (strcmp(no, stu[i].NO) == 0)//匹配成功将学生信息导入到新建链表节点
                {
                    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
                    if (newnode == NULL)
                    {
                        perror("malloc");
                        exit (1);
                    }
                    newnode->next = *head;
                    *head = newnode;
                    //将stu[i]的数据一一存到newnode中
                    newnode->one = (struct Student*)malloc(sizeof(struct Student));
if (newnode->one == NULL)
{
                perror("malloc");
                exit(1);
}
*(newnode->one) = stu[i];
break;

                }
            }
            printf("请输入学生学号\n");
        }
    }
    return *head;
}
int count = 0;//记录学生个数
struct Node* merge(struct Node* h1, struct Node* h2)
{
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    assert(head);
    head->next = NULL;
    struct Node* tail = head;
    struct Node* hh2 = h2;
    for (h1; h1 != NULL; h1 = h1->next)
    {
        for (hh2=h2; hh2 != NULL; hh2 = hh2->next)
        {
            if (strcmp(h1->one->NO,hh2->one->NO) == 0)
            {
                tail->next = h1;
                tail = h1;
                break;
            }
        }
    }
    if (head->next == NULL)
    {
        exit(1);
    }
    struct Node* turn = head->next;
    struct Node* min = head->next;
    struct Node* move = min->next;
    while (turn->next)
    {
        while (move)
        {
            if (((*(move->one)).birthday.year < (*(min->one)).birthday.year) ||
                ((*(move->one)).birthday.month <(*(min->one)).birthday.month) ||
                ((*(move->one)).birthday.day < (*(min->one)).birthday.day))
            {
                min = move;
            }
            move = move->next;
        }
        if (turn != min)
        {
            struct Student tmp = *(min->one);
            *(min->one) = *(move->one);//"引发异常“???
            *(move->one)=tmp;
        }
        turn = turn->next;
        min = turn;
        move = min->next;
    }
    return head->next;
}
int main()
{
    int i = 0;
    printf("输入第%d学生学号\n",i+1);
    while (scanf("%s", stu[i].NO) != EOF)
    {
        if (strcmp("0", stu[i].NO) == 0)
            break;
        else
        {
            printf("输入学生姓名\n");
            scanf("%s", stu[i].name);
            printf("输入学生出生日期\n");
            scanf("%d%d%d", &stu[i].birthday.year, &stu[i].birthday.month, &stu[i].birthday.day);
            i++;
            count++;
            printf("输入第%d学生学号\n", i+1);
        }
    }
    
    struct Node* Java = NULL;
    struct Node* Python = NULL;
    Java=createList(&Java, "Java");
    Python =createList(&Python, "Python");
merge(Java, Python);
    return 0;
}

img

  • 写回答

24条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2024-04-24 17:36
    关注
    获得0.45元问题酬金

    已修复!

    img

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    
    struct Date {
        int year;
        int month;
        int day;
    };
    
    struct Student {
        char NO[10];
        char name[50];
        struct Date birthday;
    } stu[50];
    
    struct Node {
        struct Student* one;
        struct Node* next;
    };
    
    int count = 0; // 记录学生个数
    
    // 创建链表
    struct Node* createList(struct Node** head, char courseName[40]) {
        assert(head);
        printf("%s", courseName);
        printf("请输入学生学号\n");
        char no[10];
        int i = 0;
        while (scanf("%s", no) != EOF) {
            if (!strcmp("0", no)) { // 输入学号为0结束输入
                break;
            } else {
                for (i = 0; i < count; i++) { // 输入的学号与stu[i]里的学号匹配
                    if (strcmp(no, stu[i].NO) == 0) { // 匹配成功将学生信息导入到新建链表节点
                        struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
                        if (newnode == NULL) {
                            perror("malloc");
                            exit(1);
                        }
                        newnode->one = (struct Student*)malloc(sizeof(struct Student)); // 为新节点的学生信息分配内存
                        if (newnode->one == NULL) {
                            perror("malloc");
                            exit(1);
                        }
                        // 将stu[i]的数据一一存到newnode中
                        strcpy(newnode->one->NO, stu[i].NO);
                        strcpy(newnode->one->name, stu[i].name);
                        newnode->one->birthday.year = stu[i].birthday.year;
                        newnode->one->birthday.month = stu[i].birthday.month;
                        newnode->one->birthday.day = stu[i].birthday.day;
    
                        newnode->next = *head;
                        *head = newnode;
                        break;
                    }
                }
                printf("请输入学生学号\n");
            }
        }
        return *head;
    }
    
    int main() {
        int i = 0;
        printf("输入第%d学生学号\n", i + 1);
        while (scanf("%s", stu[i].NO) != EOF) {
            if (strcmp("0", stu[i].NO) == 0)
                break;
            else {
                printf("输入学生姓名\n");
                scanf("%s", stu[i].name);
                printf("输入学生出生日期\n");
                scanf("%d%d%d", &stu[i].birthday.year, &stu[i].birthday.month, &stu[i].birthday.day);
                i++;
                count++;
                printf("输入第%d学生学号\n", i + 1);
            }
        }
    
        struct Node* Java = NULL;
        struct Node* Python = NULL;
        Java = createList(&Java, "Java");
        Python = createList(&Python, "Python");
    
        return 0;
    }
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月2日
  • 修改了问题 4月24日
  • 修改了问题 4月24日
  • 创建了问题 4月24日

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单