-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingTwoStacks.cpp
More file actions
101 lines (97 loc) · 1.8 KB
/
QueueUsingTwoStacks.cpp
File metadata and controls
101 lines (97 loc) · 1.8 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
#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;
class Q
{
private:
stack<int> s1;
stack<int> s2;
public:
void push(int x);
int pop();
int front();
bool empty();
};
void Q::push(int x)
{
if(s2.empty())
s2.push(x);
else
{
while(!s2.empty())
{
int tp=s2.top();
s2.pop();
s1.push(tp);
}
s1.push(x);
while(!s1.empty())
{
int tp=s1.top();
s1.pop();
s2.push(tp);
}
}
}
int Q::pop()
{
if(s2.empty())
return -1;
else
{
int tp=s2.top();
s2.pop();
return tp;
}
}
int Q::front()
{
if(s2.empty())
return -1;
else
return s2.top();
}
bool Q::empty()
{
if(s2.empty())
return true;
else
return false;
}
int main()
{
Q q1;
int x;
bool isem;
int choice;
do
{
cout<<"\t\t1.Push\n";
cout<<"\t\t2.Pop\n";
cout<<"\t\t3.Get front element\n";
cout<<"\t\t4.Is queue empty?\n";
cout<<"\t\t5.Exit\n";
cout<<"Choice [1-5] ";
cin>>choice;
switch(choice)
{
case 1: cin>>x;
q1.push(x);
break;
case 2: x=q1.pop();
cout<<"popped element : "<<x<<"\n";
break;
case 3: x=q1.front();
cout<<x<<" is at the front of queue\n";
break;
case 4: isem=q1.empty();
cout<<"Is the queue empty? "<<isem<<"\n";
break;
case 5: cout<<"Exiting...";
exit(0);
break;
}
}while(choice!=5);
return 0;
}