class StackArray
{   int a[];
    int top;
    StackArray(int size)
    {   a=new int[size];
        top=-1;
    }
    //************************
    public void push(int value)
    {   if(top==a.length-1)
        {   System.out.println("Overflow");
        }
        else
        {   top++;
            a[top]=value;
        }
    }
    //************************
    public int pop()
    {   if(top==-1)
        {   System.out.println("Underflow");
            return 0; //or return an error code like -999
        }
        else
        {   int temp=a[top];
            top--;
            return temp;
        }
    }
    //************************
    public int peek()
    {   return a[top];
    }
    //************************
    public void lifo() //for traversal
    {   for(int i=top; i>=0; i--)
        {   System.out.print(a[i]+" ");
        }
        System.out.println();
    }
    public static void main(String args[])
    {   StackArray stack=new StackArray(5);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        stack.lifo();
        stack.pop();
        stack.pop();
        stack.push(7);
        stack.lifo();
    }
}
/*Output
Overflow
5 4 3 2 1
7 3 2 1
*/
