-
Notifications
You must be signed in to change notification settings - Fork 7
/
3-3-4.cpp
85 lines (85 loc) · 1.4 KB
/
3-3-4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#define MAXSIZE 50
using namespace std;
#define ELEMTYPE int
typedef struct{
ELEMTYPE data[MAXSIZE];
int front;
int rear;
int tag;
}Queue;
void InitQue(Queue &q){
q.front=0;
q.rear=0;
q.tag=0;
}
bool EnQueue(Queue &q,ELEMTYPE x){
if(q.front==q.rear&&q.tag==1){
cout<<"error:enq"<<endl;
return false;}
else{
q.data[q.rear]=x;
q.rear=(q.rear+1)%MAXSIZE;//rear为第一个可用结点
if(q.rear==q.front){
q.tag=1;
}
return true;
}
}
bool DeQueue(Queue &q,ELEMTYPE &x){
if(q.front==q.rear&&q.tag==0)return false;
else{
x=q.data[q.front];//q为队头有值结点
q.front=(q.front+1)%MAXSIZE;
if(q.front==q.rear){
q.tag=0;
}
return true;
}
}
bool QueueEmpty(Queue q){
if(q.front==q.rear&&q.tag==0)return true;
return false;
}
int main(){
Queue q1;//客车
InitQue(q1);
Queue q2;//货车
InitQue(q2);
int x;
cout<<"客车:";
cin>>x;
while(x!=9999){
EnQueue(q1,x);
cin>>x;
}
cout<<"货车:";
cin>>x;
while(x!=9999){
EnQueue(q2,x);
cin>>x;
}
int n=0,count=0;
while(!QueueEmpty(q1)||!QueueEmpty(q2)){
n=0;
while(n<10&&(!QueueEmpty(q1)||!QueueEmpty(q2))){
if(count<4&&!QueueEmpty(q1)){
DeQueue(q1,x);
cout<<x<<" ";
count++;
n++;
}else if(!QueueEmpty(q2)){
count=0;
DeQueue(q2,x);
cout<<x<<" ";
n++;
}else if(!QueueEmpty(q1)){
DeQueue(q1,x);
cout<<x<<" ";
n++;
}
}
cout<<endl;
}
return 0;
}