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.

No comments:

Post a Comment