Reverse the Queue of type linked llist by R4R Team

How we Reverse the Queue ?
Basically, there are many method to reverse the array or queue.

Here, we reverse the queue with the use of the stack.

Process :

-Dequeue the Queue and push in stack until queue is empty
-then enqueue the queue by pop the stack until stack is empty

Following is the function which reverse the queue with the help of the queue :

void reverse()
{
while (!Queue.empty()) {
Stack.push(Queue.front());
Queue.dequeue();
}
while (!Stack.empty()) {
Queue.enqueue(Stack.top());
Stack.pop();
}
}


-Here,
push() : is used to insert element in stack
pop(): is used to remove the item from stack.
enqueue() : is used to insert the item in queue
dequeue() : is used to remove the item from the queue.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!