In Previous post, Queue Implementation using an array in java We talked and learned about the Queue data structure implementation using an array.
In this post, we will learn about Queue Implementation using LinkedList in java?
In a Queue data structure, we maintain two references, front and rear. The front points the first item of queue and rear points to the last item in the Queue.
enQueue(Obj) This operation usually adds a new node after the rear and moves rear to the next node.
deQueue() This operation usually removes the front node and moves front to the next node.
Below is the complete source code:
Generic Custom Queue implementation using Linked List:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.kkjavatutorials.queue; /** * Queue Implementation using Linked List * @author KK JavaTutorials */ public class Queue<T> { private Node<T> front; private Node<T> rear; private int length; private static class Node<T> { private final T data; private Node<T> next; public Node(T data) { this.data = data; } } /** * Method to EnQueue or insert an Item in Queue(rear) * @param data Item to be inserted in Queue */ public void enQueue(T item) { if (front == null) { rear = new Node<T>(item); front = rear; } else { rear.next = new Node<T>(item); rear = rear.next; } length++; } /** * Method to DeQueue or Remove an Item From Queue * @return return DeQueued or Removed Item from queue(front) */ public T deQueue() { if (front != null) { T item = front.data; front = front.next; length--; return item; } return null; } public int size() { return length; } public void displayQueue() { Node<T> currentNode = front; while (currentNode != null) { System.out.print(currentNode.data+" "); currentNode = currentNode.next; } } } |
Client program which uses our custom Queue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.kkjavatutorials.client; import com.kkjavatutorials.queue.Queue; /** * Client program which uses our custom Queue * @author KK JavaTutorials * */ public class ClientTest { public static void main(String[] args) { Queue<Integer> queue = new Queue<>(); queue.enQueue(10); queue.enQueue(20); queue.enQueue(30); queue.enQueue(40); queue.enQueue(50); System.out.println("Queue Length::"+queue.size()); queue.displayQueue(); System.out.println(); System.out.println("An Item deQueued ::"+queue.deQueue()); System.out.println("Queue Length::"+queue.size()); System.out.println("------------------------"); queue.displayQueue(); } } |
Output of the above program:
Queue Length::5
10 20 30 40 50
An Item deQueued ::10
Queue Length::4
————————
20 30 40 50
You May Also Like:
Java program to add two matrices
Java program to find maximum element in each row of the matrix
Java program to find maximum and minimum numbers in the given matrix
That’s all about Queue Implementation using LinkedList in java?
If you have any feedback or suggestion please feel free to drop in below comment box.