Stacks implementation in java


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



Stack 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.

Let’s see a stack of book, here the book added last in stack will be picked first.


Logic explanation of Stack in java>

Pushing item on Stack in java>

Popping element from Stack in java>



Full Program/Example of Stack implementation in java, push and pop elements in stack in java >

Below program allow you to push only int type in stack in java, you can push other data types as well, for more refer this post- Stacks (with 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);
   }
   
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* Stack class
*/
class Stack {
   private int size;
   private int[] stackAr;
   private int top; // top of stack
   /**
   * Constructor for initializing Array.
   */
   public Stack(int size) {
          this.size = size;
          stackAr = new int[size]; // Creation of Stack array
          top = -1; // initialize Stack to with -1
   }
   /**
   * Push items in stack, it will put items on top of Stack.
   */
   public void push(int 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 int 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);
   }
}
/**
*  StackExample - Main class
*/
public class StackExample {
   public static void main(String[] args) {
          Stack stack = new Stack(10); // Creation of 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 example/program, we will find that elements which were inserted last have been popped first in java.


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

So in this Data structures tutorial we learned what is Stack in java with example and program. We learned how to implement your own Stack in java. We learned how to push and pop elements in 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:-



Some of the tricky and interesting Singly LinkedList implementations in java





Implement Stack, Queue using LinkedList.

No comments:

Post a Comment