Skip to content

Commit 607a9e4

Browse files
authored
Merge pull request #1 from Alok02Jha/master
Add files via upload
2 parents 6ee79df + 551f20b commit 607a9e4

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

248.DequeueUsingArrayC++ (1).txt

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class DEQueue{
6+
private:
7+
int front;
8+
int rear;
9+
int size;
10+
int* Q;
11+
12+
public:
13+
DEQueue(int size);
14+
~DEQueue();
15+
void display();
16+
void enqueueFront(int x);
17+
void enqueueRear(int x);
18+
int dequeueFront();
19+
int dequeueRear();
20+
bool isEmpty();
21+
bool isFull();
22+
};
23+
24+
DEQueue::DEQueue(int size) {
25+
this->size = size;
26+
front = -1;
27+
rear = -1;
28+
Q = new int [size];
29+
}
30+
31+
DEQueue::~DEQueue() {
32+
delete [] Q;
33+
}
34+
35+
bool DEQueue::isEmpty() {
36+
if (front == rear){
37+
return true;
38+
}
39+
return false;
40+
}
41+
42+
bool DEQueue::isFull() {
43+
if (rear == size - 1){
44+
return true;
45+
}
46+
return false;
47+
}
48+
49+
void DEQueue::enqueueFront(int x) {
50+
if (front == -1){
51+
cout << "DEQueue Overflow" << endl;
52+
} else {
53+
Q[front] = x;
54+
front--;
55+
}
56+
}
57+
58+
void DEQueue::enqueueRear(int x) {
59+
if (isFull()){
60+
cout << "DEQueue Overflow" << endl;
61+
} else {
62+
rear++;
63+
Q[rear] = x;
64+
}
65+
}
66+
67+
int DEQueue::dequeueFront() {
68+
int x = -1;
69+
if (isEmpty()){
70+
cout << "DEQueue Underflow" << endl;
71+
} else {
72+
x = Q[front];
73+
front++;
74+
}
75+
return x;
76+
}
77+
78+
int DEQueue::dequeueRear() {
79+
int x = -1;
80+
if (rear == -1){
81+
cout << "DEQueue Underflow" << endl;
82+
} else {
83+
x = Q[rear];
84+
rear--;
85+
}
86+
return x;
87+
}
88+
89+
void DEQueue::display() {
90+
for (int i=front+1; i<=rear; i++) {
91+
cout << Q[i] << flush;
92+
if (i < rear){
93+
cout << " <- " << flush;
94+
}
95+
}
96+
cout << endl;
97+
}
98+
99+
int main() {
100+
101+
int A[] = {1, 3, 5, 7, 9};
102+
int B[] = {2, 4, 6, 8};
103+
104+
DEQueue deq(sizeof(A)/sizeof(A[0]));
105+
106+
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
107+
deq.enqueueRear(A[i]);
108+
}
109+
deq.display();
110+
deq.enqueueRear(11);
111+
112+
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
113+
deq.dequeueFront();
114+
}
115+
deq.dequeueFront();
116+
117+
cout << endl;
118+
119+
for (int i=0; i<sizeof(B)/sizeof(B[0]); i++){
120+
deq.enqueueFront(B[i]);
121+
}
122+
deq.display();
123+
deq.enqueueFront(10);
124+
deq.enqueueFront(12);
125+
126+
for (int i=0; i<sizeof(B)/sizeof(B[0]); i++){
127+
deq.dequeueRear();
128+
}
129+
deq.display();
130+
deq.dequeueRear();
131+
deq.dequeueRear();
132+
133+
return 0;
134+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <stack>
3+
4+
using namespace std;
5+
6+
class Queue{
7+
private:
8+
stack<int> e_stk;
9+
stack<int> d_stk;
10+
public:
11+
Queue(){};
12+
~Queue(){};
13+
void enqueue(int x);
14+
int dequeue();
15+
};
16+
17+
void Queue::enqueue(int x) {
18+
e_stk.push(x);
19+
}
20+
21+
int Queue::dequeue() {
22+
int x = -1;
23+
if (d_stk.empty()){
24+
if (e_stk.empty()){
25+
cout << "Queue Underflow" << endl;
26+
return x;
27+
} else {
28+
while (!e_stk.empty()){
29+
d_stk.push(e_stk.top());
30+
e_stk.pop();
31+
}
32+
}
33+
}
34+
x = d_stk.top();
35+
d_stk.pop();
36+
return x;
37+
}
38+
39+
int main() {
40+
41+
int A[] = {1, 3, 5, 7, 9};
42+
43+
Queue q;
44+
45+
cout << "Enqueue: " << flush;
46+
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
47+
q.enqueue(A[i]);
48+
cout << A[i] << flush;
49+
if (i < sizeof(A)/sizeof(A[0])-1){
50+
cout << " <- " << flush;
51+
}
52+
}
53+
cout << endl;
54+
55+
cout << "Dequeue: " << flush;
56+
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
57+
cout << q.dequeue() << flush;
58+
if (i < sizeof(A)/sizeof(A[0])-1){
59+
cout << " <- " << flush;
60+
}
61+
}
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)