3-3银行业务队列简单模拟
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3-3银⾏业务队列简单模拟
1 #include <stdio.h>
2 #include <stdlib.h>
3
4#define MAXSIZE 1000
5#define ERROR -1
6
7struct Node{
8int Customer[MAXSIZE];
9int rear;
10int front;
11 };
12
13 typedef struct Node* Queue;
14
15 Queue CreateQueue()
16 {
17 Queue Q = (Queue)malloc(sizeof(struct Node));
18 Q->front = 0;
19 Q->rear = 0;
20return Q;
21 }
22
23int IsEmpty(Queue Q)
24 {
25return Q->front == Q->rear;
26 }
27
28void AddQ(Queue Q, int X)
29 {
30if ((Q->rear + 1) % MAXSIZE == Q->front)
31 {
32 printf("The queue is full!\n");
33return;
34 }
35 Q->Customer[Q->rear] = X;
36 Q->rear = (Q->rear + 1) % MAXSIZE;
37 }
38
39int DeleteQ(Queue Q)
40 {
41if (IsEmpty(Q))
42 {
43 printf("The queue is empty!\n");
44return ERROR;
45 }
46int elem = Q->Customer[Q->front];
47 Q->front = (Q->front + 1) % MAXSIZE;
48return elem;
49 }
50
51int main()
52 {
53int N, X;
54int flag = 0;
55 Queue Q, Q1, Q2;
56 Q1 = CreateQueue();
57 Q2 = CreateQueue();
58 scanf_s("%d", &N);
59while (N--)
60 {
61 scanf_s("%d", &X);
62if (X % 2)
63 AddQ(Q1, X);
64else
65 AddQ(Q2, X);
66 }
67while (!IsEmpty(Q1) && !IsEmpty(Q2))
68 {
69if (!flag)
70 flag = 1;
71else
72 printf("");
73 printf("%d", DeleteQ(Q1));
74 printf(" %d", DeleteQ(Q1));
75 printf(" %d", DeleteQ(Q2));
76 }
77while (!IsEmpty(Q1))
78 {
79if (!flag)
80 flag = 1;
81else
82 printf("");
83 printf("%d", DeleteQ(Q1));
84 }
85
86
87while (!IsEmpty(Q2))
88 {
89if (!flag)
90 flag = 1;
91else
92 printf("");
93 printf("%d", DeleteQ(Q2));
94 }
95return0;
96 }。