Repository navigation
Expand file tree
/
Copy path25.cpp
More file actions
162 lines (134 loc) · 3.26 KB
/
Copy path25.cpp
File metadata and controls
162 lines (134 loc) · 3.26 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Dequeue using array
#include <iostream>
using namespace std;
#define MAX 5 // Maximum size of the dequeue
class Dequeue {
int arr[MAX];
int front;
int rear;
int size;
public:
Dequeue() {
front = -1;
rear = -1;
size = 0;
}
bool isFull() {
return (size == MAX);
}
bool isEmpty() {
return (size == 0);
}
// Insert element at the front
void insertFront(int key) {
if (isFull()) {
cout << "Overflow! Dequeue is full.\n";
return;
}
if (isEmpty()) {
front = 0;
rear = 0;
} else if (front == 0) {
front = MAX - 1; // Wrap around
} else {
front = front - 1;
}
arr[front] = key;
size++;
cout << "Inserted " << key << " at the front.\n";
}
// Insert element at the rear
void insertRear(int key) {
if (isFull()) {
cout << "Overflow! Dequeue is full.\n";
return;
}
if (isEmpty()) {
front = 0;
rear = 0;
} else if (rear == MAX - 1) {
rear = 0; // Wrap around
} else {
rear = rear + 1;
}
arr[rear] = key;
size++;
cout << "Inserted " << key << " at the rear.\n";
}
// Delete element from the front
void deleteFront() {
if (isEmpty()) {
cout << "Underflow! Dequeue is empty.\n";
return;
}
cout << "Deleted " << arr[front] << " from the front.\n";
if (front == rear) {
// Queue has only one element
front = -1;
rear = -1;
} else if (front == MAX - 1) {
front = 0; // Wrap around
} else {
front = front + 1;
}
size--;
}
// Delete element from the rear
void deleteRear() {
if (isEmpty()) {
cout << "Underflow! Dequeue is empty.\n";
return;
}
cout << "Deleted " << arr[rear] << " from the rear.\n";
if (front == rear) {
// Queue has only one element
front = -1;
rear = -1;
} else if (rear == 0) {
rear = MAX - 1; // Wrap around
} else {
rear = rear - 1;
}
size--;
}
// Get the front element
int getFront() {
if (isEmpty()) return -1;
return arr[front];
}
// Get the rear element
int getRear() {
if (isEmpty()) return -1;
return arr[rear];
}
// Display elements of the circular dequeue from front to rear
void display() {
if (isEmpty()) {
cout << "Dequeue is empty!\n";
return;
}
cout << "\nDequeue elements (Front to Rear): ";
int i = front;
cout<<"[";
while (true) {
cout << arr[i] << " ";
if (i == rear)
break;
i = (i + 1) % MAX; // Circular increment
}
cout<<"]\n";
}
};
int main() {
Dequeue dq;
dq.insertRear(10);
dq.insertRear(20);
dq.insertFront(5);
dq.display();
dq.deleteFront();
dq.display();
dq.insertRear(30);
dq.insertRear(40);
dq.display();
return 0;
}