-
Notifications
You must be signed in to change notification settings - Fork 1
/
248.DequeueUsingArrayC++ (1).txt
134 lines (113 loc) · 2.37 KB
/
248.DequeueUsingArrayC++ (1).txt
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
#include <iostream>
using namespace std;
class DEQueue{
private:
int front;
int rear;
int size;
int* Q;
public:
DEQueue(int size);
~DEQueue();
void display();
void enqueueFront(int x);
void enqueueRear(int x);
int dequeueFront();
int dequeueRear();
bool isEmpty();
bool isFull();
};
DEQueue::DEQueue(int size) {
this->size = size;
front = -1;
rear = -1;
Q = new int [size];
}
DEQueue::~DEQueue() {
delete [] Q;
}
bool DEQueue::isEmpty() {
if (front == rear){
return true;
}
return false;
}
bool DEQueue::isFull() {
if (rear == size - 1){
return true;
}
return false;
}
void DEQueue::enqueueFront(int x) {
if (front == -1){
cout << "DEQueue Overflow" << endl;
} else {
Q[front] = x;
front--;
}
}
void DEQueue::enqueueRear(int x) {
if (isFull()){
cout << "DEQueue Overflow" << endl;
} else {
rear++;
Q[rear] = x;
}
}
int DEQueue::dequeueFront() {
int x = -1;
if (isEmpty()){
cout << "DEQueue Underflow" << endl;
} else {
x = Q[front];
front++;
}
return x;
}
int DEQueue::dequeueRear() {
int x = -1;
if (rear == -1){
cout << "DEQueue Underflow" << endl;
} else {
x = Q[rear];
rear--;
}
return x;
}
void DEQueue::display() {
for (int i=front+1; i<=rear; i++) {
cout << Q[i] << flush;
if (i < rear){
cout << " <- " << flush;
}
}
cout << endl;
}
int main() {
int A[] = {1, 3, 5, 7, 9};
int B[] = {2, 4, 6, 8};
DEQueue deq(sizeof(A)/sizeof(A[0]));
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
deq.enqueueRear(A[i]);
}
deq.display();
deq.enqueueRear(11);
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
deq.dequeueFront();
}
deq.dequeueFront();
cout << endl;
for (int i=0; i<sizeof(B)/sizeof(B[0]); i++){
deq.enqueueFront(B[i]);
}
deq.display();
deq.enqueueFront(10);
deq.enqueueFront(12);
for (int i=0; i<sizeof(B)/sizeof(B[0]); i++){
deq.dequeueRear();
}
deq.display();
deq.dequeueRear();
deq.dequeueRear();
return 0;
}