患者到医院看病事件模拟

 实验题目(共10题, 第8题)


标题: 患者到医院看病事件模拟
时 限: 1000 ms
内存限制: 10000 K
总时限: 3000 ms
描述: 患者到医院看病的顺序是:先排队等候,再看病治疗。要求设计一个算法,模拟病人等候就诊的过程。
其中:“病人到达”用命令“A”(或“a”)表示,“护士让下一位就诊”用“N”(或“n”)表示,“不再接收病人排队”用“S”(或“s”)表示。
输入: A(或a)  病历号x
N(或n)
S(或s)
输出:

病历号为x的病人入队,无输出
若无病人,显示无病人就诊;否则输出队列中的第一个病人就诊信息
今天不再接收病人排队,输出队列中所有病人的病历号
若输入其它命令,则输出"输入命令不合法!"

输入样例:

A 123
n
n
a 124
a 125
x
s

输出样例: 病历号为123的病人就诊
无病人就诊
输入命令不合法!
今天不再接收病人排队,下列排队的病人依次就诊:124 125
提示:  
来源:
 
View Code
 1 #include<stdio.h>
2 #include<stdlib.h>
3 typedef struct Qnode
4 {
5 int date;
6 struct Qnode *next;
7 } Qnode,*Queueptr;
8 typedef struct
9 {
10 Queueptr rear;
11 Queueptr front;
12 } Linkqueue;
13 void Initqueue(Linkqueue &Q)
14 {
15 Q.front=Q.rear=(Queueptr)malloc(sizeof(Qnode));
16 if(!Q.front) exit(0);
17 Q.front->next=NULL;
18 }
19 void enqueue(Linkqueue &Q,int e)
20 {
21 Queueptr p;
22 p=(Queueptr)malloc(sizeof(Qnode));
23 if(!p) exit(0);
24 p->date=e;
25 p->next=NULL;
26 Q.rear->next=p;
27 Q.rear=p;
28 }
29 int dequeue(Linkqueue &Q,int e)
30 {
31 Queueptr p;
32 p=Q.front->next;
33 e=p->date;
34 Q.front->next=p->next;
35 if(Q.rear==p) Q.rear=Q.front;
36 free(p);
37 return e;
38 }
39 int empety(Linkqueue &Q)
40 {
41 return Q.front==Q.rear;
42 }
43 int main()
44 {
45 Linkqueue Q;
46 char q;
47 Initqueue(Q);
48 int flag=1;
49 int i;
50 while(flag)
51 {
52 scanf("%c",&q);
53 if(q=='a'||q=='A')
54 {
55 scanf("%d",&i);
56 enqueue(Q,i);
57 }
58 else if(q=='N'||q=='n')
59 {
60 if(!empety(Q))
61 {
62 printf("病历号为%d的病人就诊\n",dequeue(Q,i));
63 }
64 else
65 printf("无病人就诊\n");
66 }
67 else if(q=='s'||q=='S')
68 {
69 printf("今天不再接收病人排队,");
70 flag=0;
71 printf("下列排队的病人依次就诊:");
72 while(!empety(Q))
73 {
74 printf("%d ",dequeue(Q,i));
75 }
76 }
77 else
78 {
79 printf("输入命令不合法!\n");
80 }
81 getchar();
82 }
83 return 0;
84 }
posted @ 2011-05-24 17:53  itbird  Views(880)  Comments(0Edit  收藏  举报