usoken
11/1/2017 - 8:09 AM

Stacks - Push & Pop Operations

import java.util.Stack;

public class UsefulCodes {
  
  // STACKS, PUSH, POP
  public static void stacks(){
    
    Stack<String> stack = new Stack<String>();
    stack.push("Bottom");
    printStack(stack);
    stack.push("Second");
    printStack(stack);
    stack.push("Third");
    printStack(stack);
    //The last one goes in, goes out first when "pop" is used
    stack.pop();
    printStack(stack);
  }
  
  private static void printStack(Stack<String> s){
    if(s.isEmpty()) System.out.println("Empty Stack !");
    else System.out.printf("%s TOP of the Stack \n",s);
  }
  
  // Main Method
  public static void main(String[] args) {
    stacks();
  }
}