-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackAndQueue.cpp
More file actions
55 lines (52 loc) · 993 Bytes
/
Copy pathStackAndQueue.cpp
File metadata and controls
55 lines (52 loc) · 993 Bytes
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
#include<iostream>
#include<stack>
using std::stack;
using std::cin;
using std::cout;
using std::endl;
constexpr int MAXSIZE = 10;
class Queue{
public:
int front = 0;
int rear = 0; // 空一位以便区分满和空状态
int s[MAXSIZE];
int empty(){
return front == rear;
}
int full(){
return ((rear + 1) % MAXSIZE) == front;
}
void enQueue(int data){
if(full()){return;}
s[rear] = data;
rear = (rear + 1) % MAXSIZE;
}
int deQueue(){
if(empty()) return -1;
int t = s[front];
front++;
return t;
}
int getTop(){
if(empty()) return -1;
return s[front];
}
Queue(){
;
}
};
//实现循环队列
int main(){
Queue q;
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);
q.enQueue(6);
cout << q.deQueue();
cout << q.deQueue();
cout << q.deQueue();
q.enQueue(6);
q.enQueue(9);
cout << q.deQueue();
cout << q.deQueue();
}