Accounting Basics Tutorial


Java Collections Stack

Stack
Stack is a subclass of Vector which follows Last in and First Out.(LIFO) Order.
Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own.
Push() and Pop() are the main methods in stack class


WRITE A PROGRAM ON STACK CLASS.

import java.util.*;
class Sample
{
        static void abc(Stack <Integer>st,int a)
        {
                st.push(new Integer(a));
                System.out.println("Stack: "+st);
        }
        static void pop(Stack <Integer>st)
        {
                System.out.println("pop->");
                Integer a=st.pop();
                System.out.println(a);
                System.out.println(st);
        }
        public static void main(String args[])
        {
                Stack <Integer>st=new Stack<Integer> ();
                System.out.println("Stack"+st);
                abc(st,42);
                abc(st,52);
                abc(st,62);
                pop(st);
                pop(st);
                pop(st);

                try
                {
                        pop(st);
                }
                catch(EmptyStackException e)
                {
                        System.out.println("empty stack");
                }
        }
}



google ads place