-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.cpp
More file actions
60 lines (53 loc) · 912 Bytes
/
Copy pathdeque.cpp
File metadata and controls
60 lines (53 loc) · 912 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
56
57
58
59
60
#include <iostream>
#define SIZE 20
using namespace std;
class Deque {
private:
int deq[SIZE];
int start;
int tail;
public:
Deque() {
start = tail = 0;
}
void Push_back(int data) {
if (!IsFull()) {
deq[(tail + SIZE) % SIZE] = data;
++tail;
return;
}
cout << "队列已满,无法插入!" << endl;
}
int Pop_front() {
if (IsEmpty()) {
cout << "空队列,无法输出元素!";
return -1;
}
int data = deq[(start + SIZE) % SIZE];
++start;
return data;
}
int Size() {
return (tail - start + SIZE) % SIZE;
}
bool IsEmpty() {
return Size() == 0;
}
bool IsFull() {
return Size() == SIZE - 1;
}
};
int main()
{
Deque* deq = new Deque();
for (int i = 0; i < 21; i++) {
deq->Push_back(i);
cout << "Size is " << deq->Size() << endl;
}
for (int i = 0; i < 21; i++) {
cout << deq->Pop_front() << " ";
if ((i + 1) % 5 == 0) cout << endl;
}
delete deq;
return 0;
}