-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Reverse_Queue.cpp * Update stack.hpp
- Loading branch information
1 parent
de7f276
commit 267c042
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
An effective CPP program to reverse a Queue.In this program, | ||
a Queue is populated with values and then it is reversed using the use of Stack. | ||
Time Complexity:O(n) | ||
Space Complexity:Extra Space for Stack | ||
#include <bits/stdc++.h> | ||
*/ | ||
using namespace std; | ||
void Print(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(); | ||
} | ||
} | ||
|
||
// Main Function | ||
int main() | ||
{ | ||
queue<int> Queue; | ||
Queue.push(1); | ||
Queue.push(2); | ||
Queue.push(3); | ||
Queue.push(4); | ||
Queue.push(5); | ||
Queue.push(6); | ||
Queue.push(7); | ||
Queue.push(8); | ||
Queue.push(9); | ||
Queue.push(10); | ||
|
||
reverseQueue(Queue); | ||
Print(Queue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters