3-3 银行业务队列简单模拟

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define MAXSIZE 1000
 5 #define ERROR -1
 6 
 7  struct Node{
 8     int Customer[MAXSIZE];
 9     int rear;
10     int 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;
20      return Q;
21  }
22 
23  int IsEmpty(Queue Q)
24  {
25      return Q->front == Q->rear;
26  }
27 
28  void AddQ(Queue Q, int X)
29  {
30      if ((Q->rear + 1) % MAXSIZE == Q->front)
31      {
32          printf("The queue is full!\n");
33          return;
34      }
35      Q->Customer[Q->rear] = X;
36      Q->rear = (Q->rear + 1) % MAXSIZE;
37  }
38 
39  int DeleteQ(Queue Q)
40  {
41      if (IsEmpty(Q))
42      {
43          printf("The queue is empty!\n");
44          return ERROR;
45      }
46      int elem = Q->Customer[Q->front];
47      Q->front = (Q->front + 1) % MAXSIZE;
48      return elem;
49  }
50 
51  int main()
52  {
53      int N, X;
54      int flag = 0;
55      Queue Q, Q1, Q2;
56      Q1 = CreateQueue();
57      Q2 = CreateQueue();
58      scanf_s("%d", &N);
59      while (N--)
60      {
61          scanf_s("%d", &X);
62          if (X % 2)
63             AddQ(Q1, X);
64          else
65              AddQ(Q2, X);
66      }
67      while (!IsEmpty(Q1) && !IsEmpty(Q2))
68      {
69          if (!flag)
70              flag = 1;
71          else
72              printf(" ");
73          printf("%d", DeleteQ(Q1));
74          printf(" %d", DeleteQ(Q1));
75          printf(" %d", DeleteQ(Q2));
76      }
77      while (!IsEmpty(Q1))
78      {
79          if (!flag)
80              flag = 1;
81          else
82              printf(" ");
83          printf("%d", DeleteQ(Q1));
84      }
85 
86         
87      while (!IsEmpty(Q2))
88      {
89          if (!flag)
90              flag = 1;
91          else
92              printf(" ");
93          printf("%d", DeleteQ(Q2));
94      }
95     return 0;    
96  }

 

上一篇:循环队列原理及在单片机串口通讯的应用(一)


下一篇:不可多得的干货!docker时间同步