fushengll 2021-06-22 16:20 采纳率: 0%
浏览 19

不会写这道循环队列的

 高校实验任务安排程序设计与实现问题描述

学生选课问题中的数据元素具有如下形式:学生的自然情况包括姓名、学号、班级。2.功能要求要求完成以下功能:

⑴ 插入:将预约做实验的学生插入到合适的时间队列中;

⑵ 删除:时间队列中前5位学生可以在该时间做实验;

⑶ 查询:教师可以随时查询某个时间队列中学生的预约情况;

⑷ 修改:在没做实验之前,学生可以对预约的时间进行修改;

⑸ 输出:输出每个时间队列中预约的学生名单。

 

  • 写回答

1条回答 默认 最新

  • 个人练习生xx 2023-04-09 10:17
    关注

    根据个人理解:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student {
        char name[20];
        int id;
        char clazz[10];
        struct student *next;
    };
    
    struct student *head[5];
    
    void insert(struct student *stu, int time) {
        struct student *p = head[time];
        if (p == NULL || strcmp(stu->clazz, p->clazz) < 0) {
            stu->next = head[time];
            head[time] = stu;
        } else {
            while (p->next != NULL && strcmp(stu->clazz, p->next->clazz) > 0) {
                p = p->next;
            }
            stu->next = p->next;
            p->next = stu;
        }
    }
    
    void delete(int time) {
        for (int i = 0; i < 5 && head[time] != NULL; i++) {
            struct student *p = head[time];
            head[time] = head[time]->next;
            free(p);
        }
    }
    
    void query(int time) {
        printf("Time %d: ", time);
        struct student *p = head[time];
        while (p != NULL) {
            printf("%s(%d, %s) ", p->name, p->id, p->clazz);
            p = p->next;
        }
        printf("\n");
    }
    
    void modify(int id, int time) {
        for (int i = 0; i < 5; i++) {
            struct student *p = head[i];
            while (p != NULL) {
                if (p->id == id) {
                    struct student *q = p;
                    p = p->next;
                    q->next = NULL;
                    insert(q, time);
                    return;
                }
                p = p->next;
            }
        }
    }
    
    void output() {
        for (int i = 0; i < 5; i++) {
            query(i);
        }
    }
    
    int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            int time, choice;
            struct student *stu = (struct student *)malloc(sizeof(struct student));
            scanf("%s%d%s", stu->name, &stu->id, stu->clazz);
            scanf("%d", &choice);
            if (choice == 1) {
                scanf("%d", &time);
                insert(stu, time);
            } else if (choice == 2) {
                scanf("%d", &time);
                modify(stu->id, time);
            } else if (choice == 3) {
                output();
            } else {
                delete(i);
            }
        }
        return 0;
    }
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)