Java Program to Implement the Queue Data Structure
Queue is the fundamental data structure that follows the First-In-First-Out(FIFO) principle where the element that is inserted first is one that gets removed first. Imagine the queue of the people waiting in the line at the ticket counter: the person who arrives the first gets served first and so on.
Organization of the Queue Data Structure
The queue can be visualized as the line of the elements where elements are inserted at one end(rear) and removed from the other end(front). This structure can resemble the physical queue of the people waiting in line.

Implementation
Queue can be implemented using the arrays or linked lists. In the array-based implementation, we can use the pointers front and rear to keep track of the elements. In the linked list implementation, each node contains the data elements and the reference to the next node.
Operations Algorithms
Operation | Description | Algorithm |
---|---|---|
1. Enqueue | This operation can be used to add the element to the rear of the queue. |
|
2. Dequeue | This operation can be used to remove the element from the front of the queue. |
|
3. Peek | It can be used to get the element at the front of the queue without removing it. |
|
4. isEmpty | It can be used to check if the queue is empty or not. |
|
5. isFull | Check if the queue is full. (Is using Fixed Size Array) |
|
Example program
// Java Program to Implement
// Queue Data Structure
class Queue {
private int[] arr;
private int front;
private int rear;
private int capacity;
private int size;
// Constructor to initialize the queue
public Queue(int capacity) {
this.capacity = capacity;
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
// Insert an element at the rear of the queue
public void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is full");
return;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}
// Remove and return the element from the front of the queue
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
int removedItem = arr[front];
front = (front + 1) % capacity;
size--;
return removedItem;
}
// Return the element at the front of the queue without removing it
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty");
return -1;
}
return arr[front];
}
// Check if the queue is empty
public boolean isEmpty() {
return size == 0;
}
// Check if the queue is full
public boolean isFull() {
return size == capacity;
}
}
public class Main {
public static void main(String[] args) {
Queue queue = new Queue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println("Dequeued item: " + queue.dequeue());
System.out.println("Front item: " + queue.peek());
System.out.println("Is queue empty? " + queue.isEmpty());
}
}
Output
Dequeued item: 10 Front item: 20 Is queue empty? false
Complexity of the above Program:
Time complexity: O(1)
Space complexity: O(n) where n is the number of the elements in the queue.
Application of the Queue
- It can applies on the operating systems for the handling process scheduling.
- It can be used in the print queue management in computer systems.
- It can applies on the BFS(Breadth-First Search) algorithm in the graph traversal.
- It can applies on the handling the requests in web servers.