-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack_using_single_queue.cpp
97 lines (90 loc) · 2.4 KB
/
stack_using_single_queue.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
#include <bits/stdc++.h>
using namespace std;
class Stack
{
// Initailize objects of two in-built queues.
queue<int>q;
// To maintain current size of stack
int curr_size;
public:
Stack()
{
curr_size = 0;
}
// Add an element to the Stack
void push(int x)
{
// Get previous size of queue
int s = q.size();
// Push current element to queue
q.push(x);
// Dequeue all previous elements and Enqueue them after current element
for (int i=0; i<s; i++)
{
// This will add element at front into rear of the queue
q.push(q.front());
// This will delete element at front of the queue
q.pop();
}
curr_size++;
}
// Remove an element from the Stack
void pop(){
if (q.empty())
{
cout << "Stack is Empty, thus deletion cannot be performed\n";
return;
}
cout<<"The popped element is: "<<q.front()<<endl;
q.pop();
curr_size--;
return;
}
void top()
{
if (q.empty())
{
cout<<"Stack is Empty\n";
return;
}
cout<<"The Top Element of Stack is: "<<q.front()<<endl;
return;
}
int sizeStack()
{
cout<<"Size of the Stack: "<<curr_size<<endl;
}
};
int main()
{
Stack s;
int x, data;
do
{
cout<<"Main Menu \n";
cout<<"1. Insertion to Stack (Push Operation) \n";
cout<<"2. Deletion from Stack (Pop Operation) \n";
cout<<"3. Return Top Element from Stack \n";
cout<<"4. Size of Stack\n";
cout<<"5. Quit \n";
cout<<"Enter your choice (1-5): ";
cin>>x;
switch(x)
{
case 1: cout<<"Enter the value to be inserted in Stack: ";
cin>>data;
s.push(data);
break;
case 2: s.pop();
break;
case 3: s.top();
break;
case 4: s.sizeStack();
break;
case 5: exit(0);
default: cout<<"Invalid Choice Entered\n";
}
}
while(true);
return 0;
}