Stack
Stack
*;
import java.io.*;
interface StackOperations
{
void push(int item);
int pop();
void display();
int peek();
}
}
public int peek()
{
return st[top];
}
}
class StackTest
{
public static void main(String[] args)
{
char ch='n';
Scanner in =new Scanner(System.in);
CustomStack obj =new CustomStack(10);
do
{
System.out.println("Menu");
System.out.println("******************************");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. Display");
System.out.println("4. peek");
System.out.println("5. exit");
System.out.println("Enter your choice");
int ipt=in.nextInt();
switch(ipt)
{
case 1:
{
System.out.println("Enter the element");
int ip=in.nextInt();
obj.push(ip);
obj.display();
break;
}
case 2:
{
System.out.println("The popped element is:"+obj.pop());
break;
}
case 3:
{
obj.display();
break;
}
case 4:
{
System.out.println("The top most element is:"+obj.peek());
break;
}
default:
{
System.out.println("Invalid!!"+"Enter the choice from 1 to 4");
break;
}
}
System.out.println("Do you want to continue? (y/n)");
ch=in.next().charAt(0);
} while(ch=='y');
}
}
Output: