-
Notifications
You must be signed in to change notification settings - Fork 48
/
reverse_queue_using_stack.cpp
54 lines (44 loc) · 1.04 KB
/
reverse_queue_using_stack.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
#include <bits/stdc++.h>
using namespace std;
// Funtion to print the queue
void printQueue(queue<int>& Queue)
{
while (!Queue.empty()) {
cout << Queue.front() << " ";
Queue.pop();
}
}
// Function to reverse the queue
void reverseQueue(queue<int>& Queue)
{
stack<int> Stack;
while (!Queue.empty()) {
Stack.push(Queue.front());
Queue.pop();
}
while (!Stack.empty()) {
Queue.push(Stack.top());
Stack.pop();
}
}
// Driver code
int main()
{
//Taking size of Queue from user
int n;
cout<<"Enter the size of Queue : \n";
cin>>n;
// Taking Elements of Queue from the User
queue<int> Queue;
cout<<"\nEnter Elements of the Queue :\n";
for(int i=0;i<n;i++){
int a;
cin>>a;
Queue.push(a);
}
// Reverse the Queue Function call
reverseQueue(Queue);
//Printing the Elements of Queue Function call
cout<<"\nPrinting the Queue after reversing the Elements of Queue\n";
printQueue(Queue);
}