Stack Example in java(FILO)

import java.util.*;
public class Test{
        public static int size = 3;
    static int[] stackArray = new int[size];
    static int top=0;

        public static void push(int n)
    {
        if(top>=size)
        {
            System.out.println("Stack Overflow!!!");
        }
        else{
            stackArray[top++] = n;       
            System.out.println(n +" Added");
        }
    }
        public static void pop()
    {
    if(top>=0)
        top--;
    else
        System.out.println("Stack Underflow!!!");
    }
   
    public static void isEmpty()
    {
        if(top<0)
            System.out.println("Stack is Empty!!!");
        else
            System.out.println("Stack is not Empty :)");
    }
    public static void main(String args[])
    {
        isEmpty();
        push(2);
        push(3);
        push(4);
        push(5);
        pop();
        push(5);
        isEmpty();
    }
}
Reactions

Post a Comment

0 Comments