import java.util.Stack;
import java.util.Scanner;
public class StackDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Stack<String> stack = new Stack<>();
int choice;
while (true) {
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
String element = scanner.nextLine();
stack.push(element);
break;
case 2:
if (stack.isEmpty()) {
System.out.println("Stack is empty. Nothing to pop.");
} else {
String poppedElement = stack.pop();
}
break;
case 3:
if (stack.isEmpty()) {
System.out.println("Stack is empty. Nothing to peek.");
} else {
System.out.println("Top element of the stack is: \"" + stack.peek()+"\".");
}
break;
case 4:
if (stack.isEmpty()) {
System.out.println("Stack is empty.");
} else {
System.out.println("Stack is not empty.");
}
break;
case 5:
if (stack.isEmpty()) {
System.out.println("Stack is empty. No elements to display.");
} else {
System.out.println("Elements in the stack:");
for (String item : stack) {
System.out.println(item);
}
}
break;
case 6:
System.out.println("Exiting program.");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: