Queue with Linked List in Java
Implementing a Queue in Java using Linked Lists
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");
}






