Java Program to Reverse a Queue
How to Reverse a Queue in Java programming language?
In this we will learn how to write a Java Program to Reverse a Queue. In this page, we will learn to code and perform the task.
For example :- Input : 5 20 60 40
Output : 40 60 20 5
- First we will create a Queue.
- Initialize Queue by taking input from user.
- Call the reverse function.
The stack could help in approaching this problem. This will be a two-step process:
- Pop the elements from the queue and insert into the stack. (Topmost element of the stack is the last element of the queue)
- Pop the elements of the stack to insert back into the queue. (The last element is the first one to be inserted into the queue)
- Call show function for printing the data.
JAVA CODE :
40 60 20 5