Saturday, February 28, 2015

Queue Data Structure

Queue Data Structures in Java

Queue not only asked in interview to beginners but also asked to experienced professionals for all technologies. Queue is very important data in real world software application.

Definition : Queue is an abstract data type that follow the principle of FIFO (First-in-First-out), means the data that is inserted first will be removed first. In Queue enqueue operation (insertion of data) happen from rear and dequeue operation (removable of data) happen from from front. So in queue, in between insertion or deletion operations are not possible. If you want to remove a data from queue then it must be at front or remove all the data before your desired data and bring it at front end and remove.
We are going to discuss few examples below:-
  1. When we call to customer care of mobile service provider, and banking service provider where volume of incoming call is high, you will hear "You are are in queue, please stay online.".
  2. Shared printer also maintain the queue of printing jobs.
  3. We can see the example of queue at bank. Where people use to wait for their turn to perform the transaction.
Implementation of Queue

Here we are going to implement queue in java. We will handle all the possible case like Queue overflow and underflow.Here list of basic operations those we are going to perform.
  1. add : This operation will insert the element in queue.
  2. remove : This operation will return the front element and remove it from queue.
  3. Size : It will return the number of elements in queue.
  4. isEmpty : This will return true if there is no element in queue else false.
 Queue can be implemented using array, linked list or stack. Here we are going to implement queue using array. We will also implement queue using linked list and stack but in another article.

We will discuss the algorithm step by step.

 Initially front and rear both point to 0. We will discuss why we are pointing front and rear to 0 when we check the condition of empty.

Step I          

Step II :   

Step III :  

Step IV : 

Step V :  

Step VI :

Step VII :



Explanation :

Here in step I we are pointing front and rear to 0, just to show that we do not have any data to dequeue and data to be inserted at position zero.

Now we discuss two basic operation enqueue and dequeue and we also discuss what is the size of queue(number of elements in queue) and when will be the queue empty. 

1. isEmpty : 
    
    Check from any queue and try to remove element one by one and find the condition where queue does not contain any element in it.

See at Step III Queue:
 front points at 2 and reat points at 8
 remove an element and increase front by 1, so  now front points at 3.
 remove one more element and increase front by 1, now front points at 4.
 likewise when front points at 7 and rear points at 8, we remove one element from position 7 increase  front by one. Now front and rear both are pointing to 8 and queue is empty.

So when front == rear then queue is empty.

 Follow the same procedure and check your queue when it is getting empty.
  
isEmpty
  return front==rear

2. Size :

    We are going to find the number of elements in queue at a particular time and that would be size of queue. Here we are checking boundary condition for overflow queue by total capacity minus one, so our modulus operation will work too.

Size 
    (capacity - front + rear) modulus  (capacity)

3. Enqueue
 
    If size of queue is equal to (capacity - 1) then return queue overflow else insert an element in queue and increase rear by one.

4. Dequeue
 
    If queue is empty return queue underflow else return element from front and increase front by one.

Exception Java class


package com.company.dataStructure.exception;

/**
 * @author 
 *
 */
public class QueueException extends RuntimeException {

 private static final long serialVersionUID = -7622234016049976464L;

 public QueueException() {
  super();
 }

 /**
  * @param message
  */
 public QueueException(String message) {
  super(message);
 }

 /**
  * @param cause
  */
 public QueueException(Throwable cause) {
  super(cause);
 }

 /**
  * @param message
  * @param cause
  */
 public QueueException(String message, Throwable cause) {
  super(message, cause);
 }
}

Queue implementation


package com.company.dataStructure.imp;

import com.company.dataStructure.exception.QueueException;

/**
 * @author 
 *
 */
public class Queue {

 private Object queue[];
 private int front = 0;
 private int rear = 0;
 private int capacity;
 
 /**
  *This default constructor will initialize the capacity with size 10.  
  */
 public Queue() {
  this(10);
 }
 /**
  *This constructor will initialize the object array with size
  * capacity+1.
  *We are increasing the user given capacity by one to check 
  *the condition of queue overflow.   
  */
 public Queue(int capacity) {
  this.capacity = capacity + 1;
  this.queue = new Object[this.capacity];
 }
 /**
  * This method will accept an element and store into queue and throw 
  * QueueException if queue is full.
  */
 public void add(T e){
  
  /* This condition will check availability of space 
   to store another element in queue.We need to keep 
   one space empty in queue to check the overflow 
    condition because if front and rear point to 
    same location then it will be condition like empty.
     */
  
  if(this.size() == this.capacity - 1){
   throw new QueueException("Queue overflow.");
  }else{
   queue[this.rear] = e;
   
   /*We are increasing the rear by one and taking the
     modulus of that with capacity.By this way we are 
     initializing the rear to next empty position in 
     queue. Check the case moving from Step IV to Step
      V and observe the use of modulus.
    */
   
   this.rear = (this.rear + 1) % this.capacity;
  }
 }

 /**
  * This method will remove the front element from queue and
  throw QueueException if queue is empty.
  */
 public T remove(){
  if(this.isEmpty()){
   throw new QueueException("Queue underflow.");
  }else{
   @SuppressWarnings("unchecked")
   T tempElement = (T)this.queue[this.front];
   this.queue[this.front] = null;
   
   /*We are increasing the front by one and taking the 
    modulus of that with capacity. By this way 
    we are initializing the front to next position 
    in queue.
    */
   
   this.front = (this.front +1 ) % (this.capacity);
   return tempElement;
  }
 }
 /**
  * This method will return the number of elements in queue.
  * 
  */
 public int size(){
  return (
    (this.capacity - this.front + this.rear) 
     % 
    (this.capacity)
    );
 }

 /**
  * 
  * This method will return true if queue is empty else false. 
  */
 public boolean isEmpty(){
  return this.front == this.rear;
 }
 
 /* Overridden this method to print the queue*/
 @Override
 public String toString() {

  StringBuilder builder = new StringBuilder();
  for (int i = 0; i < queue.length; i++) {
   builder.append(queue[i]+" ");
  }
  return "front : "+this.front+" rear : "+this.rear
    +"\n"+builder.toString();
 }
}

Java Class with main method to test Queue


package com.company.test;

import com.company.dataStructure.imp.Queue;


public class MainQueue {

/**
* @param args
*/
public static void main(String[] args) {
Queue<Integer> queue = new Queue<Integer>();
System.out.println(queue.size()+" : "+queue.isEmpty());
for (int i = 1; i < 11; i++) {
System.out.print("Size Befor : "+queue.size()+" : "+i);
queue.add(i);
System.out.println(" : Size After : "+queue.size());
}
System.out.println(queue);
System.out.print("\n\n"+queue.size()+" : ");
System.out.println(queue.isEmpty()+"\n\n");
for (int i = 1; i < 5; i++) {
System.out.print("Size Befor : "+queue.size()+" : ");
System.out.println(queue.remove()+" : Size After : "+queue.size());
}
queue.add(100);
System.out.println(queue);
}
}

Thursday, February 12, 2015

Data Structure in Java : Stack Data Structure in Java

Data Structure in Java : Stack Data Structure in Java: Data structures are essential of computer science and used in most computer applications. Here we are going to discuss basic of data stru...

Wednesday, February 11, 2015

Stack Data Structure in Java


Data structures are essential of computer science and used in most computer applications. Here we are going to discuss basic of data structure and we will implement them.

First data structure we are going to discuss here is stack.

1. Stack

Definition : Stack is a data structure where insertion and deletion of data happen only from one end. So stack data structure follow the LIFO principle, means last in first out.

Example of Stack:-

  1. Stack of plates on dining table.
  2. Female wears bangles that follow the last in first out principle same as stack.
  3. Technical implemented undo is the best example of implementation of stack.
Here we are going to implement stack in java. We will handle all the possible case like stack overflow and stack underflow.Here list of basic operations those we are going to perform.

  1. Push : This operation will insert the element in stack.
  2. Pop : This operation will return the top element and remove it from stack.
  3. Peek : This operation will return the top element but not remove it from stack.
  4. Size : It will return the number of elements in stack.
  5. isEmpty : This will return true if there is no element in stack else false.
Note : I have implemented the stack using generics of java.

 Below are the java classes for implementation.


  • Java class to handle the exception in case of stack overflow and underflow.

package com.company.dataStructure.exception;

/**
 * This class will be used to handle the stack overflow and underflow exception.
 *
 */
public class StackException extends RuntimeException {

private static final long serialVersionUID = -1255380634583675380L;

/**
*
*/
public StackException() {
}

/**
* @param message
*/
public StackException(String message) {
super(message);
}

/**
* @param cause
*/
public StackException(Throwable cause) {
super(cause);
}

/**
* @param message
* @param cause
*/
public StackException(String message, Throwable cause) {
super(message, cause);
}
}


  • Java class of stack implementation.

package com.company.dataStructure.imp;


import com.company.dataStructure.exception.StackException;

public class Stack<T>{
private Object element[];
private int size = -1;
private int capacity;
/**
*<p>This default constructor will initialize the object array with size 10.<p>
*/
public Stack(){
this(10);
}
/**
*<p>This constructor will initialize the object array with size capacity.<p>
*/
public Stack(int capacity){
this.capacity = capacity;
this.element = new Object[this.capacity];
}
/**
* <p>This method will accept an element and store into stack.<p>
*/
public void push(T e){
if((this.size+1) == this.capacity){
throw new StackException("Stack overflow.");
}
this.element[++size] = e;
}
/**
* <p>This method will return the top element from stack and remove it from stack. If there is no element in stack, it will through underflow stackException.<p>
*/
public T pop(){
if(this.isEmpty()){
throw new StackException("Stack underflow.");
}
@SuppressWarnings("unchecked")
E tempElement = (E)this.element[size];
element[size--] = null;
return tempElement;
}
/**
* <p>This method will return the top element from stack. If there is no element in stack, it will through underflow stackException.<p>
*/
public T peek(){
if(isEmpty()){
throw new StackException("Stack underflow.");
}
@SuppressWarnings("unchecked")
T tempElement = (T)this.element[size];
return tempElement;
}
/**
* <p>This method will return true if stack has no element else false.<p>
* <p>Here we are comparing the size with 0 (zero) because we are using size for index and if value of size is zero then there would be a data in stack.<p>
*/
public boolean isEmpty(){
return this.size<0;
}
/**
* <p>This method will return the number of element in the stack.<p>
*/
public int size(){
return this.size+1;
}

}



  • A java class to test the data stack structure.


package com.company.test;

import com.company.dataStructure.imp.Stack;


public class TestStack {

/**
* @param args
*/
public static void main(String[] args) {

Stack<String> stack = new Stack<String>();
               /*Here <String> mean stack will accept only string type data. If you want to use stack for  int type data then use Integer */
stack.push("Country");
stack.push("State");
stack.push("District");
System.out.println(stack.size());
System.out.println(stack.isEmpty());
System.out.println(stack.pop());
System.out.println(stack.size());
System.out.println(stack.peek());
System.out.println(stack.size());
System.out.println(stack.isEmpty());
}
}



Exercise after learning this, implement stack where no upper bound, means stack can accept any number of elements.

Please post your comments and valuable suggestions to make learning easy to all of us.