Java Program to Implement the Queue Data Structure
Last Updated :
27 May, 2024
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.
| - Check if the Queue is full.
- If Queue is full then display an overflow message as output.
- If Queue is not full then increment the rear pointer to the next position.
- Insert the new element at the rear position.
- Update the size of the queue.
|
---|
2. Dequeue
| This operation can be used to remove the element from the front of the queue.
| - Check whether if the queue is empty or not.
- If queue is empty then display an underflow message as output.
- If queue is not empty then retrieve the element at the front position.
- Increment the front pointer to the next position.
- Update the size of the queue.
- Return the retrieved element as output.
|
---|
3. Peek
| It can be used to get the element at the front of the queue without removing it.
| - Check whether if the queue is empty.
- If queue is empty then display an overflow message as output.
- If queue is not empty then return the element at front position without removing it.
|
---|
4. isEmpty
| It can be used to check if the queue is empty or not.
| - Check whether if the size of the queue is equal to zero.
- if the size is 0 then it return true(that means queue is empty).
- Otherwise, returns false.
|
---|
5. isFull
| Check if the queue is full. (Is using Fixed Size Array)
| - Check if size of the queue is equal to capacity of the array.
- If size is equal to capacity then return true.
- Otherwise, returns false.
|
---|
Example program
Java
// 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());
}
}
OutputDequeued 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.
Similar Reads
JavaScript program to implement queue using stack A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to
3 min read
JavaScript program to implement stack using queue In this article, we implement a JavaScript program to make a stack using a queue data structure. It provides essential stack methods like push(), pop(), and peek(), isEmpty() operations, utilizing either one or two queues to simulate the behavior of a stack. Examples: Input:push(2)push(3)pop()peek()
4 min read
Introduction to Queue Data Structure Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the first element inserted is the first to be popped out. FIFO Principle in Queue:FIFO Principle states that the first element added to the Queue will be the first one to be removed or processed. So, Queue is like
5 min read
What is Queue Data Structure? What is Queue Data Structure?A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at
2 min read
C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read