-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.cpp
102 lines (93 loc) · 2.22 KB
/
Deque.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#define MAX 3 // Change the MAX of the deque here
using namespace std;
template <class T>
class Deque {
private:
T dq[MAX];
int f, r;
public:
Deque() {
f = -1;
r = -1;
}
bool isFull() {
return (f == 0 && r == MAX - 1) || (f == r + 1);
}
bool isEmpty() {
return f == -1;
}
void insertf(T x) {
if (isFull()) {
cout << "Deque is full! Cannot insert at f " << x << endl;
} else {
if (f == -1) {
f = 0;
r = 0;
} else if (f == 0) {
f = MAX - 1;
} else {
f--;
}
dq[f] = x;
cout << x << " inserted at f" << endl;
}
}
void insertr(T x) {
if (isFull()) {
cout << "Deque is full! Cannot insert at r " << x << endl;
} else {
if (f == -1) {
f = 0;
r = 0;
} else if (r == MAX - 1) {
r = 0;
} else {
r++;
}
dq[r] = x;
cout << x << " inserted at r" << endl;
}
}
void deletef() {
if (isEmpty()) {
cout << "Deque is empty! Cannot delete from f" << endl;
} else {
cout <<dq[f] << " deleted from f" << endl;
if (f == r) {
f = -1;
r = -1;
} else if (f == MAX - 1) {
f = 0;
} else {
f++;
}
}
}
void deleter() {
if (isEmpty()) {
cout << "Deque is empty! Cannot delete from r" << endl;
} else {
cout << dq[r] << " deleted from r" << endl;
if (f == r) {
f = -1;
r = -1;
} else if (r == 0) {
r = MAX - 1;
} else {
r--;
}
}
}
};
int main() {
Deque<int> dq;
dq.insertf(1);
dq.insertr(2);
dq.insertf(3);
dq.insertr(4);
dq.deletef();
dq.deleter();
dq.insertf(5);
return 0;
}