Stack
Stack
*;
class Stack {
public int capacity;
public int top = -1;
public int[] a;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. Push 2. Pop 3. Peek 4. Display 5. Exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter element to push:");
int data = sc.nextInt();
stack.push(data);
break;
case 2:
int popped = stack.pop();
if (popped != -1) {
System.out.println("Popped element: " + popped);
}
break;
case 3:
int peeked = stack.peek();
if (peeked != -1) {
System.out.println("Peeked element: " + peeked);
}
break;
case 4:
stack.display();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
}