Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Implement the queue data structure
A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In, First Out (FIFO).
In a queue, the element added first will be removed first. The end where elements are added is called the rear, and the end where elements are removed is called the front.
Scenario 1
Let us see an example, scenario:
Input: [150, 300, 450, 600] After removing an element from the queue: Output: [300, 450, 600]
The following are the ways to implement a queue in Java:
- Using Array
- Using a Linked List
Implementing a Queue Using an Array
We will use an array to implement the queue. The array will have a fixed size, and we will maintain two pointers: one for the front and one for the rear of the queue.
Algorithm
Steps to implement a queue using an array:
- Initialize an array of a fixed size.
- Maintain two pointers: front and rear.
- Implement methods to add elements to the rear, remove elements from the front, and check if the queue is empty or full.
- Handle the circular nature of the queue by wrapping around the pointers when they reach the end of the array.
Example
java program to implement a queue using an array:
import java.util.Arrays;
public class QueueUsingArr {
private int[] arr;
private int front;
private int rear;
private int capacity;
private int size;
public QueueUsingArr(int capacity) {
this.capacity = capacity;
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public void enqueue(int element){
if(size == capacity){
System.out.println("Queue is full. Cannot enqueue " + element);
return;
}
rear = (rear + 1) % capacity;
arr[rear] = element;
size++;
System.out.println("Enqueued: " + element);
}
public int dequeue() {
if(size == 0){
System.out.println("Queue is empty. Cannot dequeue.");
return -1;
}
int element = arr[front];
front = (front + 1) % capacity;
size--;
System.out.println("Dequeued: " + element);
return element;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
public void display() {
if(isEmpty()) {
System.out.println("Queue is empty.");
return;
}
System.out.print("Queue: ");
for(int i = 0; i < size; i++) {
System.out.print(arr[(front + i) % capacity] + " ");
}
System.out.println();
}
public static void main(String[] args) {
QueueUsingArr queue = new QueueUsingArr(5);
queue.enqueue(150);
queue.enqueue(300);
queue.enqueue(450);
queue.enqueue(600);
queue.display();
queue.dequeue();
queue.display();
queue.enqueue(750);
queue.display();
queue.dequeue();
queue.display();
}
}
When you run the above code, the output will be:
Enqueued: 150 Enqueued: 300 Enqueued: 450 Enqueued: 600 Queue: 150 300 450 600 Dequeued: 150 Queue: 300 450 600 Enqueued: 750 Queue: 300 450 600 750 Dequeued: 300 Queue: 450 600 750
Implementing a Queue Using a Linked List
We can also implement a queue using a linked list. In this approach, we will maintain a reference to the front and rear nodes of the linked list. This allows us to dynamically add and remove elements without worrying about a fixed size.
Algorithm
Steps to implement a queue using a linked list:
- Create a Node class to represent each element in the queue.
- Maintain references to the front and rear nodes of the linked list.
- Implement methods to add elements to the rear, remove elements from the front, and check if the queue is empty.
- Handle the case when the queue becomes empty after removing an element.
- Ensure that the linked list is properly updated when elements are added or removed.
Example
The following is a Java program to implement a queue using a linked list:
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class QueueUsingLinkedList {
private Node front;
private Node rear;
private int size;
public QueueUsingLinkedList() {
front = null;
rear = null;
size = 0;
}
public void enqueue(int element) {
Node newNode = new Node(element);
if (rear == null) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
size++;
System.out.println("Enqueued: " + element);
}
public int dequeue() {
if (front == null) {
System.out.println("Queue is empty. Cannot dequeue.");
return -1;
}
int element = front.data;
front = front.next;
if (front == null) {
rear = null; // If the queue becomes empty
}
size--;
System.out.println("Dequeued: " + element);
return element;
}
public boolean isEmpty() {
return front == null;
}
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return;
}
Node current = front;
System.out.print("Queue: ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
QueueUsingLinkedList queue = new QueueUsingLinkedList();
queue.enqueue(150);
queue.enqueue(300);
queue.enqueue(450);
queue.display();
queue.dequeue();
queue.display();
queue.enqueue(600);
queue.display();
queue.dequeue();
queue.display();
}
}
When you run the above code, the output will be:
Enqueued: 150 Enqueued: 300 Enqueued: 450 Queue: 150 300 450 Dequeued: 150 Queue: 300 450 Enqueued: 600 Queue: 300 450 600 Dequeued: 300 Queue: 450 600