Java – Stack Collection

Stack:

  • Stack is an extension of Vector class.
  • It follows LIFO – Last In First Out Rule

Methods are:

  1. boolean empty() : Tests the stack is empty or not
  2. Object peek(): returns the top element of stack but not remove
  3. Object pop(): returns the top element of stack and removes
  4. void push(Object e): push element on to the stack
import java.util.*;
class Code
{
            public static void main(String[] args) throws Exception {
                        Stack<Integer> stk = new Stack<Integer>();
                        stk.push(10);
                        stk.push(20);
                        stk.push(30);
                        stk.push(40);
                        System.out.println(“Stack is : ” + stk);
 
                        System.out.println(“Pop : ” + stk.pop());
                        System.out.println(“Pop : ” + stk.pop());
                        System.out.println(“Stack is : ” + stk);
 
                        stk.push(50);
                        stk.push(60);
                        System.out.println(“Stack is : ” + stk);
 
                        System.out.println(“Peek : ” + stk.peek());
                        System.out.println(“Peek : ” + stk.peek());
                        System.out.println(“Stack is : ” + stk);
            }
}

Stack Operations – Menu Driven Program

import java.util.*;
class Code
{
            public static void main(String[] args) throws Exception
            {
                        Scanner sc = new Scanner(System.in);
                        Stack<Integer> stk = new Stack<Integer>();
                        while(true)
                        {
                                    System.out.println(“1.Push \n2.Pop \n3.Display \n4.Peek \n5.Quit”);
                                    System.out.print(“Enter choice : “);
                                    int ch = sc.nextInt();
                                    if(ch==1)
                                    {
                                                System.out.print(“Enter element to push : “);
                                                int ele = sc.nextInt();
                                                stk.push(ele);
                                                System.out.println(“Element Pushed”);
                                    }
                                    else if(ch==2)
                                    {
                                                if(stk.empty())
                                                            System.out.println(“Empty stack”);
                                                else
                                                            System.out.println(“Pop : ” + stk.pop());
                                    }
                                    else if(ch==3)
                                    {
                                                if(stk.empty())
                                                            System.out.println(“Empty stack”);
                                                else
                                                            System.out.println(“Stack is : ” + stk);
                                    }
                                    else if(ch==4)
                                    {
                                                if(stk.empty())
                                                            System.out.println(“Empty stack”);
                                                else
                                                            System.out.println(“Peek : ” + stk.peek());
                                    }
                                    else if(ch==5)
                                    {
                                                System.out.println(“End”);
                                                System.exit(1);
                                    }
                                    else
                                                System.out.println(“Invalid choice”);
                        }
            }
}
Scroll to Top