Saturday, November 3, 2012

Simple Queue implementation in Java

Here is a small demo of a simple queue implementation in Java

We have 3 classes for this demo

1. Node.java : which is the actual element in the queue

2. Queue.java : actual queue implementation - which does insertion at the end (tail) and deletion from the front (head)

3. MainClass.java: The class which has the main method which constructs the queue

Node.java

package com.giri.queue;

public class Node {
 private int data;
 private Node theNodeBehind;
 
 public Node() {
  data = -1;
  theNodeBehind = null;
 }
 
 public Node(int data, Node theNodeBehind) {
  this.data = data;
  this.theNodeBehind = theNodeBehind;
 }

 public int getData() {
  return data;
 }

 public void setData(int data) {
  this.data = data;
 }

 public Node getTheNodeBehind() {
  return theNodeBehind;
 }

 public void setTheNodeBehind(Node theNodeBehind) {
  this.theNodeBehind = theNodeBehind;
 }
  
}

Queue.java

package com.giri.queue;

public class Queue {
 private Node head;
 private Node tail;
 private int totalNodeCount;
 
 public Queue() {
  head = null;
  tail = null;
  totalNodeCount = 0;
 }
  
 public Node getHead() {
  return head;
 }



 public void setHead(Node head) {
  this.head = head;
 }



 public Node getTail() {
  return tail;
 }



 public void setTail(Node tail) {
  this.tail = tail;
 }



 public int getTotalNodeCount() {
  return totalNodeCount;
 }



 public void setTotalNodeCount(int totalNodeCount) {
  this.totalNodeCount = totalNodeCount;
 }



 public void insert(int data) {
  Node newNode = new Node(data, null);
    
  if(totalNodeCount == 0) {
   
   head = newNode;
   tail = newNode;
   
  } else {
   getTail().setTheNodeBehind(newNode);
   setTail(newNode);
  }
  totalNodeCount++;
  System.out.println("\n Node that is inserted: " + data + " , after insertion the current node count = " + totalNodeCount);
 }
 
 public Node pop() {
  
  if(totalNodeCount == 0) {
   System.out.println("\nQueue is empty ! Nothing to pop out...");
   return null;
  }
  Node temp = getHead();
  Node newHead = getHead().getTheNodeBehind();
  setHead(newHead);
  totalNodeCount--;
  
  System.out.println("\n Node that is popping out: "+ temp.getData() + " , current node count after popping = " + totalNodeCount);
  
  return temp;
 }
 
 public void display() {
  if(totalNodeCount == 0) {
   System.out.println("\nQueue is empty !");
   return;
  }
  
  System.out.println("\n Displaying Queue Contents: \n");
  
  Node currentNode = getHead();
  while(currentNode != null) {
   System.out.print(" ["+currentNode.getData()+"] <<-- ");
   currentNode = currentNode.getTheNodeBehind();
  }
  if(currentNode == null) {
   System.out.print("NULL");
  }
  
  System.out.println("\n\n [ Head = (" + getHead().getData() + ") ] and [ Tail = (" + getTail().getData() + ") ]");
 }
}

MainClass.java

package com.giri.queue;

public class MainClass {
 
 private static Queue q;

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  q = new Queue();
  
  q.display();
  
  q.insert(1);
  q.display();
  
  q.insert(2);
  q.display();
  
  q.insert(3);
  q.insert(4);
  q.insert(5);
  q.insert(6);
  q.insert(7);
  q.insert(8);
  q.insert(9);
  q.insert(10);
  
  q.display();
  
  q.pop();
  q.display();
  
  q.pop();
  q.pop();
  q.pop();
  q.pop();
  q.display();

 }

}

Output


Queue is empty !

 Node that is inserted: 1 , after insertion the current node count = 1

 Displaying Queue Contents: 

 [1] <<-- NULL

 [ Head = (1) ] and [ Tail = (1) ]

 Node that is inserted: 2 , after insertion the current node count = 2

 Displaying Queue Contents: 

 [1] <<--  [2] <<-- NULL

 [ Head = (1) ] and [ Tail = (2) ]

 Node that is inserted: 3 , after insertion the current node count = 3

 Node that is inserted: 4 , after insertion the current node count = 4

 Node that is inserted: 5 , after insertion the current node count = 5

 Node that is inserted: 6 , after insertion the current node count = 6

 Node that is inserted: 7 , after insertion the current node count = 7

 Node that is inserted: 8 , after insertion the current node count = 8

 Node that is inserted: 9 , after insertion the current node count = 9

 Node that is inserted: 10 , after insertion the current node count = 10

 Displaying Queue Contents: 

 [1] <<--  [2] <<--  [3] <<--  [4] <<--  [5] <<--  [6] <<--  [7] <<--  [8] <<--  [9] <<--  [10] <<-- NULL

 [ Head = (1) ] and [ Tail = (10) ]

 Node that is popping out: 1 , current node count after popping = 9

 Displaying Queue Contents: 

 [2] <<--  [3] <<--  [4] <<--  [5] <<--  [6] <<--  [7] <<--  [8] <<--  [9] <<--  [10] <<-- NULL

 [ Head = (2) ] and [ Tail = (10) ]

 Node that is popping out: 2 , current node count after popping = 8

 Node that is popping out: 3 , current node count after popping = 7

 Node that is popping out: 4 , current node count after popping = 6

 Node that is popping out: 5 , current node count after popping = 5

 Displaying Queue Contents: 

 [6] <<--  [7] <<--  [8] <<--  [9] <<--  [10] <<-- NULL

 [ Head = (6) ] and [ Tail = (10) ]

No comments :

Post a Comment