Stacks (with Generics) in java


In this Data structures tutorial we will learn what is generic Stack in java with example and program. We will learn how to implement your own generic Stack in java.

Stacks is Collection of entities or items.


Adding item in Stack is called PUSH.
Removing item from stack is called POP.
Push and pop operations happen at Top of stack.
Stack follows LIFO (Last in first out) - means last added element is removed first from stack.

For more explanation and diagram of Stack. Refer Stacks in java.


Full Program/Example(of Stack implementation using Generics) in java >
/**
*Exception to indicate that Stack is full.
*/
class StackFullException extends RuntimeException {
  
   public StackFullException(){
       super();
   }
   
   public StackFullException(String message){
       super(message);
   }
   
}
/**
*Exception to indicate that Stack is empty.
*/
class StackEmptyException extends RuntimeException {
  
   public StackEmptyException(){
       super();
   }
   
   public StackEmptyException(String message){
       super(message);
   }
   
}
/**
* Stack class(generic type)
*/
class Stack<T> {
   private int size;
   private T[] stackAr;
   private int top; // top of stack
   /**
   * Constructor for initializing Array.
   */
   @SuppressWarnings("unchecked")
   public Stack(int size) {
          this.size = size;
          stackAr = (T[])new Object[size]; //Creation of Generic Stack Array
          top = -1; // initialize Stack to with -1
   }
   /**
   * Push items in stack, it will put items on top of Stack.
   */
   public void push(T value){
          if(isFull()){
                 throw new StackFullException("Cannot push "+value+", Stack is full");
          }
          stackAr[++top] = value;
   }
   /**
   * Pop items in stack, it will remove items from top of Stack.
   */
   public T pop() {
          if(isEmpty()){
                 throw new StackEmptyException("Stack is empty");
          }
          return stackAr[top--]; // remove item and decrement top as well.
   }
   /**
   * @return true if Stack is empty
   */
   public boolean isEmpty(){
          return (top == -1);
   }
   /**
   * @return true if stack is full
   */
  
   public boolean isFull(){
          return (top == size - 1);
   }
}
 
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* Main class - StackExampleGeneric
*/
public class StackExampleGeneric {
   public static void main(String[] args) {
          Stack<Integer> stack = new Stack<Integer>(10); // Creation of Generic Stack
          stack.push(11);
          stack.push(21);
          stack.push(31);
          stack.push(41);
          stack.push(51);
         
          System.out.print("Popped items: ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
         
   }
}
/** OUTPUT
Popped items: 51 41 31 21 11
*/

If we analyze output of above generic stack example, we will find that elements which were inserted last have been popped first in java.


Complexity of generic Stack in java>
Push - O(1) [as we push element at Top]
Pop - O(1) [as popping is done at Top too]

So in this Data structures tutorial we learned what is generic Stack in java with example and program. We learned how to implement your own generic Stack in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Stacks, Queues in java


Singly LinkedList implementations in java:-



Doubly LinkedList implementations in java:-



Implement Stack, Queue using LinkedList.


eEdit
Must read for you :