Implementing Own Stack in Java 3

Lets implement own Abstract Data Type : Stack.
So stack is an abstract data type which support LIFO i.e last in first out.

Lets now have a look what are the operation it will support:

1. new -> making a new stack
2. push -> insert an object
3. pop -> remove the top object
4. top -> give top object without removing it.

Supporting method:
———————-
1. size -> size of stack.
2. isEmpty -> whether stack is empty or not.

Now lets define an interface which will have the above methods declaration.


public interface Stack {

       //to get the size of the stack
       public int size();

       //check whether stack is empty or not
       public boolean isEmpty();

       // To get the top element of the stack without removing it.
       public Object top() throws StackEmptyException;

       // To get the top element of the stack after removing it from stack
       public Object pop() throws StackEmptyException;

       //To push the element inside stack.
       public void push(Object obj) throws StackOverflowException ;

}

Above top, pop and push are throwing two exceptions i.e. StackEmptyException, StackOverflowException
Lets make these Exceptions classes:


public class StackEmptyException extends RuntimeException{

       public StackEmptyException(){
             super ();
      }

       public StackEmptyException(String message){
             super (message);
      }

       public StackEmptyException(String message, Throwable cause){
             super (message, cause);
      }
}


public class StackOverflowException extends RuntimeException {

       public StackOverflowException(){
             super ();
      }

       public StackOverflowException(String message){
             super (message);
      }

       public StackOverflowException(String message, Throwable cause){
             super (message, cause);
      }
}

Both the above are runtime classes that’s why they extends RuntimeException class.

Now lets make the implementation class of Stack Interface.


public class ArrayBasedStack implements Stack{

       private static final int capacity = 8;
       private Object[] s;
       private final int N; // capacity
       private int top = -1;

       public ArrayBasedStack(){
             this (capacity );
      }

       public ArrayBasedStack( int capacity){
             N = capacity;
             s = new Object[ N];
      }

       public int size() {
             return top + 1;
      }

       public boolean isEmpty() {
             return top < 0 ? true : false ;
      }

       public Object top() throws StackEmptyException {
             if (isEmpty())
                   throw new StackEmptyException( "Stack is Empty");
             return s [top ];
      }

       public Object pop() throws StackEmptyException {
             if (isEmpty())
                   throw new StackEmptyException( "Stack is Empty");
             Object item = s[ top];
             s[ top --] = null ;
             return item;
      }

       public void push(Object obj) throws StackOverflowException {
             if (size() == N )
                   throw new StackOverflowException( "Stack is Full");
             s[++ top ] = obj;
      }

}

Lets Test our stack implementation with a main class:


public class RunStack {

       public static void main(String[] args) {
            Stack stack = new ArrayBasedStack();
             //stack.pop();
            System. out .println(stack.isEmpty());
            stack.push( new Integer(4));
            System. out .println(stack.top());
            System. out .println(stack.size());
            System. out .println(stack.pop());
            System. out .println(stack.isEmpty());
            System. out .println(stack.pop());
      }

}

The output of above is :

true
4
1
4
true
Exception in thread "main" com.algo.stack.StackEmptyException : Stack is Empty
      at com.algo.stack.ArrayBasedStack.pop( ArrayBasedStack.java:36 )
      at com.algo.stack.RunStack.main( RunStack.java:14 )

3 thoughts on “Implementing Own Stack in Java

  1. Reply SRR Aug 12,2013 12:57 pm

    Don’t see any reason to use a self made stack like yours. There is already a stack in the jdk that has much more features and is coded in a better way. So what’s your intention?

  2. Reply oppanblogs Aug 12,2013 6:18 pm

    Hi SRR,

    You are absolutely right. The intention is that if we thought of abstract data type stack, what are the main operation comes in our mind and how to implement them without using the JDK Stack.

    You are right there is already a stack in the jdk that has much more features and is coded in a better way. 🙂

  3. Reply Massage Aug 29,2013 7:05 am

    I cannot thanks enough for the blog.Many thanks. Keep writing.

Leave a Reply