Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Reverse_Queue.cpp #120

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Data Structures/C:C++/Stack/Reverse_Queue.cpp
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);
}
2 changes: 1 addition & 1 deletion Data Structures/C:C++/Stack/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct stackmp{
ll_node<T> *new_node = create_node(new_data);
ll_node<T> *temp_node = this->top;
new_node->next = temp_node;
this->top = new_node;
this->top = new_node; // here "this" pointer is used to access the current class's object's parameters
size++;
}

Expand Down