forked from Sushreesatarupa/DSA-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackUsingQueue.cpp
More file actions
134 lines (124 loc) · 2.81 KB
/
stackUsingQueue.cpp
File metadata and controls
134 lines (124 loc) · 2.81 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
#include<iostream>
using namespace std;
class queue{
private:
int arry[50], front = -1, rear = -1, temp, returnNum;
int *F = arry;
int *R = arry;
public:
void enQueue(int num){
if(rear > 48){
cout << "\nQueue OverFlow\n";
}
else{
if(front == -1 && rear == -1){
*R = num;
R++;
front++;
rear++;
}
else{
*R = num;
R++;
rear++;
}
}
}
int deQueue(){
if( (front > rear) || front < 0){
//cout << "\nQueue UnderFlow\n";
front = -1;
rear = -1;
F = arry;
R = arry;
return -1;
}
else{
returnNum = *F;
F++;
front++;
return returnNum;
}
return -1;
}
void display(){
if(front > rear){
cout << "\nThe Queue is Empty.\n";
}
else{
//cout <<"\n" << front << " | " << rear;
cout << "\nThe Queue is as Follows: ";
for(int i = rear; i >= front; i--){
cout << arry[i] << " ";
}
}
}
};
class stack{
public:
queue q1, q2;
int selected = 1;
void push(int num){
int x = 0;
if(selected == 1){
q1.enQueue(num);
while (x != -1){
x = q2.deQueue();
if(x != -1)
q1.enQueue(x);
}
x = 0;
selected = 2;
}
else{
q2.enQueue(num);
while (x != -1){
x = q1.deQueue();
if(x != -1)
q2.enQueue(x);
}
x = 0;
selected = 1;
}
}
int pop(){
if(selected == 1){
cout << "\nThe Deleted element is : " << q2.deQueue();
}
else{
cout << "\nThe Deleted element is : " << q1.deQueue();
}
}
int display(){
if(selected == 1){
q2.display();
}
else{
q1.display();
}
}
};
int main(){
int n;
char c;
stack s1;
while(1){
cout << "\nEnter 1 for push and 2 for pop : ";
cin >> c;
switch (c){
case '1':
cout << "\nEnter the Num: ";
cin >> n;
s1.push(n);
s1.display();
break;
case '2':
s1.pop();
s1.display();
break;
default:
cout << "You Entere a wrong Choice, Try Again:";
break;
}
}
}