其他分享
首页 > 其他分享> > 看病要排队

看病要排队

作者:互联网

/*
题意:就是个优先队列,优先级高的先出队,相同优先级的先来先出队,求出队顺序。
思路:每个医生都维护一个有顺序的链表队列,在插入的时候插入适当的顺序。
*/
//Accepted Code:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4;
struct edge {
    int id, num;
};
typedef edge ElemType;
typedef struct ListPtr {
    ElemType data;
    ListPtr *next;
}*lists;
int len[MAXN];
lists head[MAXN], tail[MAXN];
void Alloc(lists &p) {
    p = (ListPtr *)malloc(sizeof(ListPtr));
    p -> next = NULL;
}
void link() {
    for (int i = 0; i < MAXN; i++) {
        len[i] = 0;
        Alloc(head[i]);
        tail[i] = head[i];
    }
}
void push(int k, ElemType e) {
    lists p, r = head[k];
    Alloc(p);
    p -> data = e;
    while (r -> next != NULL && e.num <= r -> next -> data.num)//优先级高的在前面
        r = r -> next;
    p -> next = r -> next;
    r -> next = p;
    len[k]++;
}
void pop(int k) {
    len[k]--;
    lists p = head[k] -> next;
    head[k] -> next = p -> next;
    if (p == tail[k])
        tail[k] = head[k];
    free(p);
}
ElemType front(int k) {//队头
    return head[k] -> next -> data;
}
int main() {
    char opt[5];
    int n, apt, bpt;
    while (~scanf("%d", &n)) {
        link();
        int top = 1;
        for (int i = 0; i < n; i++) {
            scanf("%s%d", opt, &apt);
            if (opt[0] != 'O') {
                scanf("%d", &bpt);
                push(apt, (edge){top++, bpt});
            }
            else {
                if (len[apt]) {
                    printf("%d\n", front(apt).id);
                    pop(apt);
                }
                else printf("EMPTY\n");
            }
        }
    }
    return 0;
}

 

标签:head,看病,int,排队,apt,next,len,lists
来源: https://blog.csdn.net/lzyws739307453/article/details/96619258