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
Implentation:
- First we will create a Queue.
- Initialize Queue.
- 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 to reverse a queue using stack
Method 1
Method 2
Method 1
Run
import java.util.*;
class Queue
{
int front, rear, size;
int capacity;
int[] array;
public Queue (int capacity)
{
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
// Queue is full when size becomes
// equal to the capacity
boolean isFull (Queue queue)
{
return (queue.size == queue.capacity);
}
// Queue is empty when size is 0
boolean isEmpty (Queue queue)
{
return (queue.size == 0);
}
// Method to add an item to the queue.
// It changes rear and size
void enqueue (int item)
{
if (isFull (this))
return;
this.rear = (this.rear + 1) % this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
}
int dequeue ()
{
if (isEmpty (this))
return Integer.MIN_VALUE;
int item = this.array[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}
public Queue queuereverse (Queue q)
{
Stack < Integer > stk = new Stack <> ();
while (!q.isEmpty (q))
{
stk.add (q.array[front]);
q.dequeue ();
}
while (!stk.isEmpty ())
{
q.enqueue (stk.peek ());
stk.pop ();
}
return q;
}
public void disp () /* function to display the elements of the queue */
{
if (front == -1)
{
System.out.print ("\nQueue is Empty");
}
else
{
for (int i = front; i <= rear; i++)
{
System.out.print (array[i] + " ");
}
}
System.out.println ();
}
}
// Driver class
public class Main
{
public static void main (String[]args)
{
Queue queue = new Queue (1000);
queue.enqueue (10);
queue.enqueue (20);
queue.enqueue (30);
queue.enqueue (40);
System.out.println ("Original queue : ");
queue.disp ();
System.out.println ();
queue = queue.queuereverse (queue);
System.out.println ("Queue after reversing : ");
queue.disp ();
System.out.println ();
}
}
Output: Original queue : 10 20 30 40 Queue after reversing : 40 30 20 10
Method 2
Run
import java.util.*;
// Java program to reverse a queue
public class Main
{
//function to print the queue
static void disp (Queue q) /* function to display the elements of the queue */
{
if (q.isEmpty ())
{
System.out.print ("\nQueue is Empty");
}
else
{
while (!q.isEmpty ())
{
System.out.print (" " + q.peek ());
q.remove ();
}
}
System.out.println ();
}
// Function to reverse the queue using stack
static void reversequeue (Queue q)
{
Stack < Integer > stack = new Stack <> ();
while (!q.isEmpty ())
{
stack.add ((Integer) q.peek ());
q.remove ();
}
while (!stack.isEmpty ())
{
q.add (stack.peek ());
stack.pop ();
}
}
public static void main (String args[])
{
Queue < Integer > queue;
queue = new LinkedList < Integer > ();
queue.add (10);
queue.add (20);
queue.add (30);
queue.add (40);
reversequeue (queue);
System.out.println ("Queue after reversing :");
disp (queue);
}
}
Output: Queue after reversing : 40 30 20 10
Java code to reverse a queue using recursion
Method 1
Method 2
Method 1
Run
import java.util.*;
class Queue
{
int front, rear, size;
int capacity;
int array[];
public Queue (int capacity)
{
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
// Queue is full when size becomes
// equal to the capacity
boolean isFull (Queue queue)
{
return (queue.size == queue.capacity);
}
// Queue is empty when size is 0
boolean isEmpty (Queue queue)
{
return (queue.size == 0);
}
// Method to add an item to the queue.
// It changes rear and size
void enqueue (int item)
{
if (isFull (this))
return;
this.rear = (this.rear + 1) % this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
}
int dequeue ()
{
if (isEmpty (this))
return Integer.MIN_VALUE;
int item = this.array[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}
public Queue reverseQueue (Queue q)
{
// Base case
if (q.isEmpty (q))
return q;
// Dequeue current item
int data = q.array[front];
q.dequeue ();
// Reverse remaining queue
q = reverseQueue (q);
// Enqueue current item
q.enqueue (data);
return q;
}
public void disp () /* function to display the elements of the queue */
{
if (front == -1)
{
System.out.print ("\nQueue is Empty");
}
else
{
for (int i = front; i <= rear; i++)
{
System.out.print (array[i] + " ");
}
}
System.out.println ();
}
}
// Driver class
public class Main
{
public static void main (String[]args)
{
Queue queue = new Queue (1000);
queue.enqueue (10);
queue.enqueue (20);
queue.enqueue (30);
queue.enqueue (40);
System.out.println ("Original queue : ");
queue.disp ();
System.out.println ();
queue.reverseQueue (queue);
System.out.println ("Queue after reversing : ");
queue.disp ();
System.out.println ();
}
}
Output : Original queue : 10 20 30 40 Queue after reversing : 40 30 20 10
Method 2
Run
import java.util.*;
public class Main
{
static Queue < Integer > queue;
//Function to print the queue
static void disp ()
{
while (!queue.isEmpty ())
{
System.out.print (queue.peek () + " ");
queue.remove ();
}
}
//Function to reverse the queue using recursion
static Queue < Integer > reverseQueue (Queue < Integer > q)
{
// Base case
if (q.isEmpty ())
return q;
// Dequeue current item (from front)
int data = q.peek ();
q.remove ();
// Reverse remaining queue
q = reverseQueue (q);
// Enqueue current item (to rear)
q.add (data);
return q;
}
// Driver code
public static void main (String args[])
{
queue = new LinkedList < Integer > ();
queue.add (10);
queue.add (20);
queue.add (30);
queue.add (40);
queue = reverseQueue (queue);
System.out.println ("Queue after reversing :");
disp ();
}
}
Output : Queue after reversing : 40 30 20 10
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java

Login/Signup to comment