forked from Keshav002/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueUsingStack.cpp
More file actions
121 lines (103 loc) · 1.58 KB
/
Copy pathqueueUsingStack.cpp
File metadata and controls
121 lines (103 loc) · 1.58 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
# include<iostream>
using namespace std;
class Stack
{
int top;
public:
int a[10];
Stack()
{
top = -1;
}
void push(int x);
int pop();
bool isEmpty();
};
void Stack::push(int x)
{
if(top >= 10)
{
cout << "Stack Overflow \n";
}
else
{
a[++top] = x;
}
}
int Stack::pop()
{
if(top < 0)
{
cout << "Stack Underflow \n";
return 0;
}
else
{
int d = a[top--];
return d;
}
}
bool Stack::isEmpty()
{
if(top < 0)
{
return true;
}
else
{
return false;
}
}
class Queue {
public:
Stack S1, S2;
void enqueue(int x);
int dequeue();
};
void Queue :: enqueue(int x)
{
S1.push(x);
cout << "Element "<<x<<" Inserted into Queue\n";
}
int Queue :: dequeue()
{
int x, y;
while(!S1.isEmpty())
{
x = S1.pop();
S2.push(x);
}
y = S2.pop();
while(!S2.isEmpty())
{
x = S2.pop();
S1.push(x);
}
return y;
}
int main()
{
int val, value;
Queue q;
do{
cout<<"Enter 1 for Enqueue: \n";
cout<<"Enter 2 for Dequeue: \n";
cout<<"Enter 3 for Exit: \n";
cin>>val;
if(val == 1){
cout<<"Enter the value you want to enqueue: ";
cin>>value;
q.enqueue(value);
}
else if(val == 2){
cout << "Removing element " << q.dequeue()<<" from queue\n" ;
}
else if(val == 3){
exit;
}
else{
cout<<"Wrong choice";
}
}while(val!=3);
return 0;
}