Implementation of Generics Queue using Linked List in Java [Generics Queue Implementation Using Linked List ]π₯π₯
πDATA STRUCTURE [ QUEUE IMPLEMENTATION ]
This post shows how to code in JAVA to implementing a Generic Queue using the Linked list.
So, lets start with basic definition Queueπ
What is the Queue?
Queue: A data structure called queue stores and retrieves data in the order of its arrival. A queue is also called a FIFO(First-In-First-Out) list.
Queue is a list of elements in which an element is inserted at one end and deleted from the other end of the queue.
Elements are inserted at the rear end and deleted from the front end
The following two operations perform on queue-
- Insertion (enqueue)
- Deletion (dequeue)
Generics:
Java Generics is a set of related methods or a set of similar types. Generics allow types Integer, String, or even user-defined types to be passed as a parameter to classes, methods, or interfaces. Generics are mostly used by classes like HashSet or HashMap.
Generics Advantages in java |
Generics ensure compile-time safety which allows the programmer to catch the invalid types while compiling the code.
Generics Queue Implementation Using Linked List Complete Codeπ
class QueueImplUsingLinkedList<E>{
private Node front;
private Node rear;
private int length;
public QueueImplUsingLinkedList(){
this.front= null;
this.rear = null;
this.length = 0;
}
//Creating a Node type class, In Linked List a Node has two parts- data part and address/reference part.
class Node{
private E data;
Node next;
public Node(E data){
this.data = data;
}
}
// length method is created to count the items in Queue
public int length(){
return length;
}
// isEmpty method is used to check the Queue , has an item or not in the Queue.
public boolean isEmpty(){
return length==0;
}
// This method is used to insert an item at rear end of Queue .
public void enqueue(E data){
Node temp = new Node(data);
if(isEmpty())
front=temp;
else
rear.next = temp;
rear =temp;
length++;
}
//This method is used to show the items in the Queue list.
public void show(){
if(isEmpty()){
System.out.println("Queue is empty!!");
}
Node curr = front ;
System.out.print("Queue Elements : ");
while(curr!= null){
System.out.print(curr.data+" -->");
curr = curr.next;
}
System.out.println("null");
}
//This method is used to delete an item from the front end Queue list.
public E dequeue(){
if(isEmpty())
System.out.println("Stack is empty!!");
E result = front.data;
front = front.next;
if(front == null){
rear = null;
}
length--;
return result;
}
// This method returns first element of the Queue list.
public void front(){
if(isEmpty())
System.out.println("Queue is Empty!!");
else
System.out.println("First element :"+front.data);
}
// This method returns last element of the Queue list.
public void rear(){
if(isEmpty())
System.out.println("Queue is Empty!!");
else
System.out.println("Last element :"+rear.data);
}
//main method
public static void main(String aa[]){
//QueueImplUsingLinkedList <Number> queue = new QueueImplUsingLinkedList<Number>();
QueueImplUsingLinkedList <Object> queue = new QueueImplUsingLinkedList<Object>();
queue.enqueue("p2d");
queue.enqueue('A');
queue.enqueue(10);
queue.enqueue(20.0);
queue.enqueue(30.12f);
queue.enqueue(40);
queue.enqueue(50);
queue.show();
System.out.println("Length of queue :"+queue.length());
queue.front();
queue.rear();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.show();
System.out.println("Length of queue :"+queue.length());
queue.front();
queue.rear();
}
}
Note: To run this code in your system ,Save this program with QueueImplUsingLinkedList.java
0 Comments
if you have any doubts, Please let me know.