Stack is an abstract data type that serves as a collection of elements, with

Two main operations:

  • Push, which adds an element to the collection, and
  • Pop, which removes the most recently added element that was not yet removed?
Image-source: Wikipedia.org

Additionally, a peek operation can, without modifying the stack, return the value of the last element added. Calling this structure a stack is by analogy to a set of physical items stacked one atop another, such as a stack of plates.

The order in which an element added to or removed from a stack is described as last in, first out, referred to by the acronym LIFO[last in first out]. As with a stack of physical objects, this structure makes it easy to take an item off the top of the stack, but accessing a datum deeper in the stack may require taking off multiple other items first.

In simple words, stack is a linear data structure where insertion(push) and deletion(pop) done from ‘Top of the Stack’(last end).

Real life examples: plates stack. Into a box we can adds new plates from upper side and we don’t remove any plates from middle or below. In stack same thing happened.

Methods

push() -> inserts new element into stack from top of the stack.
pop() -> removes existing element from top of the stack.
peek() -> gets top of the stack element without removing(deleting).
isEmpty() -> checks stack is empty or not.

Let’s apply stack using collections framework.

  1. Write a code, to perform PUSH operations into stack.
import java.util.Stack;
 
public class stack1 {
    public static void main(String[] args) {
        Stack <Integer> stk = new Stack<>();
        stk.push(20);
        stk.push(10);
        stk.push(40);
       
        System.out.println(“stack -> “+stk);
 
        stk.push(50);
        System.out.println(“stack -> “+stk);
 
        stk.push(80);
        System.out.println(“stack -> “+stk);
 
    }
}
output

2. Write a code, to perform POP operation into stack.

import java.util.Stack;
 
public class stack2 {
 public static void main(String[] args) {
        Stack <Integer> stk = new Stack<>();
        stk.push(20);
        stk.push(10);
        stk.push(40);
        stk.push(60);
        stk.push(80);
       
        System.out.println(“stack -> “+stk);
 
        stk.pop();
        System.out.println(“stack -> “+stk);
      
        stk.pop();
        System.out.println(“stack -> “+stk);
 
    }
}
output

3. Write a code, to perform peek operation into stack.

Note: in this operation, we only get’s top of the stack element

// integer stack
import java.util.Stack;
 
public class stack3 {
    public static void main(String[] args) {
        Stack <Integer> stk = new Stack<>();
        stk.push(20);
        stk.push(10);
        stk.push(40);
        stk.push(60);
        stk.push(80);
       
        System.out.println(“stack -> “+stk);
        System.out.println(“peek element -> “+stk.peek());
       
        stk.pop();
        System.out.println(“stack -> “+stk);
        System.out.println(“peek element -> “+stk.peek());
      
        stk.pop();
        System.out.println(“stack -> “+stk);
        System.out.println(“peek element -> “+stk.peek());
      
    }
}
output

let’s see another implementation

// string stack
import java.util.Stack;
 
public class stack4{
    public static void main(String[] args) {
        Stack <String> stk = new Stack<>();
        stk.push(“tata”);
        stk.push(“gaana”);
        stk.push(“microcodes”);
        stk.push(“mobiles”);
        stk.push(“projects”);
       
        System.out.println(“stack -> “+stk);
        System.out.println(“peek element -> “+stk.peek());
       
        stk.pop();
        System.out.println(“stack -> “+stk);
        System.out.println(“peek element -> “+stk.peek());
      
        stk.pop();
        System.out.println(“stack -> “+stk);
        System.out.println(“peek element -> “+stk.peek());
      
    }
}
output

4. Write a code, to check a stack is empty or not.

import java.util.Stack;
 
public class stack5 {
     public static void main(String[] args) {
        Stack <Integer> stk = new Stack<>();
        stk.push(20);
        stk.push(10);
        stk.push(40);
       
        System.out.println(“stack -> “+stk);
        System.out.println(“stack size -> “+stk.size());
 
       stk.pop();
       System.out.println(“stack size -> “+stk.size());
       
       stk.pop();
       System.out.println(“stack size -> “+stk.size());
       
       stk.pop();
       System.out.println(“stack size -> “+stk.size());
       
       if (stk.isEmpty()) {
        System.out.println(“stack is empty!!”);
       }
 
    }
}
output