STACKS USING ARRAYS
import java.io.*; class StackArray { public static void main (String args[])throws IOException { BufferedReader br = new ...
https://www.computersprofessor.com/2016/06/stacks-using-arrays.html?m=0
import java.io.*;
class StackArray
{
public static void
main (String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER
SIZE OF STACK");
int
n=Integer.parseInt(br.readLine());
int
stack[]=new int[n];
int
top=-1,ch;
do
{
System.out.println("1: PUSH 2 : POP 3: DISPLAY 4:
EXIT");
System.out.println("ENTER
YOUR CHOICE");
ch=Integer.parseInt(br.readLine( ));
switch(ch)
{
case
1:
if(top < n-1)
{
System.out.println("ENTER ELEMENT TO BE
INSERTED");
stack[++top] =
Integer.parseInt(br.readLine( ));
}
else
{
System.out.println("STACK
IS OVER FLOW");
}
break;
case
2:
if(top != -1)
{
System.out.println("ELEMENT"+stack[--top]+"DELETED");
}
else
{
System.out.println("STACK
IS UNDER FLOW");
break;
}
break;
case
3: if(top != -1)
{
for(int
i=top; i > 0; i--)
{
System.out.println(stack[i]);
}
}
break;
}
}while(ch < 4);
}
}
OUTPUT:
ENTER SIZE OF STACK 5
1: PUSH 2 : POP 3: DISPLAY 4: EXIT
ENTER YOUR CHOICE 1
ENTER ELEMENT TO BE INSERTED
1
1: PUSH 2 : POP 3: DISPLAY 4: EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO BE INSERTED
2
1: PUSH 2 : POP 3: DISPLAY 4: EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO BE INSERTED
3
1: PUSH 2 : POP 3: DISPLAY 4: EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO BE INSERTED
4
1: PUSH 2 : POP 3: DISPLAY 4: EXIT
ENTER YOUR CHOICE
3
4
3
2
1: PUSH 2 : POP 3: DISPLAY 4: EXIT
ENTER YOUR CHOICE 4