Sitemap

Queue with Linked List in Java

Implementing a Queue in Java using Linked Lists

--

Press enter or click to view image in full size
Photo by Caspar Camille Rubin on Unsplash

What is Queue?

1- Enqueue method

Add an element at the rear(tail) of the queue(linked list).

 //enqueue method -> add data at rear(tail)
public void enQueue(Object data){
//new node
Node newNode=new Node(data);
if(isEmpty()){
front=newNode;
rear=newNode;
}
else{
getRear().nextNode=newNode;
rear=getRear();
}
size++;
}(

Get Rafia Shaikh | Byte School’s stories in your inbox

Join Medium for free to get updates from this writer.

2- Dequeue method

Remove the current element at the front(head) of the queue(linked list).


//dequeue -> delete first node
public Object deQueue(){
if(isEmpty())
return null;
else{
Node tempNode=front;
front=tempNode.nextNode;
tempNode.nextNode=null;
return tempNode.data;
}
}

3- Display queue method

Access the current element at the front to rear of the queue.

//Display method
public void displayQueue(){
Node tempNode=front;
System.out.print("[");
while(tempNode!=null){
System.out.print(tempNode.data+",");
tempNode=tempNode.nextNode;
}
System.out.print("]\n");
}

Complete code

GitHub — Rafia-Shaikh-eng/Data-Structures-and-Algorithms-in-Java: Basic and advance tutorials of the DSA in java

Recored Lecture

(56) Rafia Shaikh — YouTube

Press enter or click to view image in full size
Photo by Debby Hudson on Unsplash

--

--

Rafia Shaikh | Byte School
Rafia Shaikh | Byte School

Written by Rafia Shaikh | Byte School

Data scientist and teacher passionate about demystifying data. Sharing practical knowledge and insights to help others navigate the world of data.