-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path3-2-1.cpp
54 lines (54 loc) · 864 Bytes
/
3-2-1.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
#include<iostream>
#define MAXSIZE 10
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;
}
}
int main(){
Queue q;
InitQue(q);
int x;
cin>>x;
while(x!=9999){
EnQueue(q,x);
cin>>x;
}
while(DeQueue(q,x)){
cout<<x<<" ";
}
cout<<endl;
return 0;
}