-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.cpp11.cpp
More file actions
98 lines (96 loc) · 1.84 KB
/
Copy pathdeque.cpp11.cpp
File metadata and controls
98 lines (96 loc) · 1.84 KB
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
#include <iostream>
using namespace std;
#define SIZE 5
int deque[SIZE];
int front = -1, rear = -1;
void insertFront(int x) {
if ((front == 0 && rear == SIZE - 1) || (front == rear + 1)) {
cout << "Deque Overflow\n";
return;
}
if (front == -1) {
front = rear = 0;
}
else if (front == 0) {
front = SIZE - 1;
}
else {
front--;
}
deque[front] = x;
cout << x << " inserted at front\n";
}
void insertRear(int x) {
if ((front == 0 && rear == SIZE - 1) || (front == rear + 1)) {
cout << "Deque Overflow\n";
return;
}
if (rear == -1) {
front = rear = 0;
}
else if (rear == SIZE - 1) {
rear = 0;
}
else {
rear++;
}
deque[rear] = x;
cout << x << " inserted at rear\n";
}
void deleteFront() {
if (front == -1) {
cout << "Deque Underflow\n";
return;
}
cout << deque[front] << " deleted from front\n";
if (front == rear) {
front = rear = -1;
}
else if (front == SIZE - 1) {
front = 0;
}
else {
front++;
}
}
void deleteRear() {
if (rear == -1) {
cout << "Deque Underflow\n";
return;
}
cout << deque[rear] << " deleted from rear\n";
if (front == rear) {
front = rear = -1;
}
else if (rear == 0) {
rear = SIZE - 1;
}
else {
rear--;
}
}
void display() {
if (front == -1) {
cout << "Deque is empty\n";
return;
}
cout << "Deque elements: ";
int i = front;
while (true) {
cout << deque[i] << " ";
if (i == rear)
break;
i = (i + 1) % SIZE;
}
cout << endl;
}
int main() {
insertRear(10);
insertRear(20);
insertFront(5);
display();
deleteFront();
deleteRear();
display();
return 0;
}