Stackop Test Dedt
Stackop Test Dedt
import java.util.Scanner;
class stackop
{
int max=10;
int top=0;
int[] a= new int[max];
void push(int x)
{
if(top==max-1)
{
System.out.println("Overflow");
}
else
{
a[top]=x;
top++;
}
}
int pop()
{
if(top<0)
{
System.out.println("Underflow");
return -1;
}
else{
top--;
return(a[top+1]);
}
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
stackop stk= new stackop();
int choice=0;
int x;
while(choice<3)
{
System.out.println("1.Push\n2.Pop\n3.Exit");
choice=scan.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the element u want to push:");
x=scan.nextInt();
stk.push(x);
break;
case 2:
if(stk.pop()!=-1)
{
System.out.println(stk.pop());
}
break;
case 3:
break;
}
}
}
}
Output:
1.Push
2.Pop
3.Exit
1
Enter the element u want to push:
2
1.Push
2.Pop
3.Exit
2
1.Push
2.Pop
3.Exit
2
Underflow
1.Push
2.Pop
3.Exit
3