Queue Implementation Using Linked List in Java
Last Updated :
27 May, 2024
Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the constraints of the fixed-size data structure like an array-based queue.
Organization of Queue Using Linked List
In the linked list-based queue, each node contains two parts: the data and the pointer to the next node. The queue has two main pointers:
- Front: The front can point to the first node of the linked list.
- Rear: Rear can point to the last node of the linked list.

Initially, when the queue is empty, both the front and rear point to the null. As the new nodes are added, the rear is to be updated to the point to the latest node and when the nodes are removed, the front is to be updated to the point to the next node in line.
Basic Operations of Queue
Operation | Description | Algorithm |
---|
1. Enqueue(adding an elements) | The enqueue operation can be used to add the element to the rear of the queue.
| - Create the new node with the given value.
- If the queue is empty then set both the front and rear pointers to the new node.
- Otherwise set the next pointer of a current rear node to the new node and update the rear pointer to the new node.
|
---|
2. Dequeue(Removing an element) | The dequeue operation can be used to removes the element from the front of the queues.
| - Check if the queue is empty then return the message indicating the queue is empty.
- Store the value of the front node.
- Update the front pointer to next node.
- If front becomes NULL set the real to NULL.
- Delete the old front node.
- Return the stored value.
|
---|
3. Peek | The peek operation can be used to allow the look at the element at the front of queue without removing it.
| - Check if the queue is empty then return the message indicating the queue is empty.
- Return the data of the front node.
|
---|
4. IsEmpty | This operation checks the whether the queue is empty which is determined by the checking if the front pointer is null.
| - Return whether the front pointer is NULL.
|
---|
5. Size | This operation can be used to calculating the size of the queue involves the traversing from the front to the rear and counting the number of the nodes.
| - Initialize the count variable to 0.
- Traverse the queue from the front to the rear.
- For the each node increment the count by 1.
- Return the count.
|
---|
Implementation of the Queue using Linked List
To the implement the queue using the linked list in java, We need two classes: Node and LinkedListQueue.
- Node: This class can represents the individual elements of the queue.
- LinkedListQueue: This class will be handle the queue operations using the Node objects.
Example program:
Java
// Java Program to Implement Queue
// Using Linked List
// Node Class
class Node {
int data;
Node next;
// Constructor
public Node(int data) {
this.data = data;
this.next = null;
}
}
// Queue Class
class Queue {
Node front, rear;
int length;
// Constructor
public Queue() {
this.front = this.rear = null;
this.length=0;
}
// Adding Element in the LinkedList/Queue
void enqueue(int key) {
this.length++;
Node temp = new Node(key);
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
this.rear.next = temp;
this.rear = temp;
}
// Removing Element from the top
void deque() {
if (this.front == null)
return;
Node temp = this.front;
this.front = this.front.next;
// If Queue is Empty
if (this.front == null)
this.rear = null;
// Removing the Front Element
temp.next = null;
}
// Checking on Front Element
int peek() {
if (this.front != null)
return this.front.data;
// Return Exception
return Integer.MIN_VALUE;
}
// Return the Size of Queue
int size(){
return this.length;
}
// Printing the Elements of Queue
void printQueue(){
Node temp=this.front;
System.out.print("Element of Queue : ");
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
}
// Driver Class
public class Main {
// Main Function
public static void main(String[] args) {
Queue q = new Queue();
q.enqueue(10);
q.enqueue(20);
q.printQueue();
System.out.println("Size :" + q.size());
q.deque();
q.deque();
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.printQueue();
System.out.println("Size :" + q.size());
System.out.println("Front item is: " + q.peek());
}
}
OutputElement of Queue : 10 20
Size :2
Element of Queue : 30 40 50
Size :5
Front item is: 30
Time and Space Complexity
Time complexity: O(1)
Space complexity: O(n), where n is number of nodes in the linked list.
Applications of the Queue
- It can applied in Event-driven simulation and Queues are used to the manage events in the time-based simulation systems.
- It can applies on the Job scheduling and it can used to queues the job scheduling tasks in the operating system.
- It can applies on Data buffering. Queue serve as the buffer in the data processing and transmission systems.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read