-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueStack.cpp
119 lines (106 loc) · 2.09 KB
/
QueueStack.cpp
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
#include <iostream>
using namespace std;
class stack{
private:
int size, i = -1, temp, t;
int arry[50];
int *top = arry;
public:
void push(int num){
if (i > 48){
cout << "Stack Overflow";
}
else{
*top = num;
top++;
i++;
temp = i;
}
}
char pop(){
i = temp;
if(i <= -1){
return -1;
}
else{
t = arry[i];
top--;
i--;
return t;
}
}
void display(){
temp = i;
//cout << "| " << i << " |";
while (temp != -1){
cout << arry[temp] << " ";
temp--;
}
}
stack copy(stack s) {
stack st;
temp = i;
while (temp != -1){
st.push(s.pop());
temp--;
}
return st;
}
};
class queue{
public:
stack s1,s2;
void enqueue(int num){
s1.push(num);
}
void dequeue(){
int x = 0;
//s2 = s1.copy(s1);
while(x != -1){
x = s1.pop();
if(x != -1)
s2.push(x);
}
s2.pop();
cout << "Deleted element: " << s2.pop();
while(s1.pop() != -1){}
x = 0;
while( x != -1){
x = s2.pop();
if(x != -1)
s1.push(x);
}
}
void display(){
cout << "\n|-";
s1.display();
cout << "-|";
cout << "\n<-";
s2.display();
cout << "->";
}
};
int main(){
int n;
char c;
queue q1;
while(1){
cout << "\nEnter 1 for enqueue and 2 for dequeue : ";
cin >> c;
switch (c){
case '1':
cout << "\nEnter the Num: ";
cin >> n;
q1.enqueue(n);
q1.display();
break;
case '2':
q1.dequeue();
q1.display();
break;
default:
cout << "You Entere a wrong Choice, Try Again:";
break;
}
}
}