import java.util.Scanner; //Author: SIRA, CHAD MICHAEL || Date Created: 10.12.23 class Node { int data; Node next; Node prev; Node(int data) { this.data = data; next = null; prev = null; } } public class StackLinkedList { private Node top; public StackLinkedList() { top = null; } public void push(int item) { Node newNode = new Node(item); if (top == null) { top = newNode; } else { newNode.next = top; top.prev = newNode; top = newNode; } System.out.println("Pushed: " + item); } public void pop() { if (top != null) { int item = top.data; top = top.next; if (top != null) { top.prev = null; } System.out.println("Popped: " + item); } else { System.out.println("The stack is empty. Cannot pop."); } } public void top() { if (top != null) { System.out.println("Top element: " + top.data); } else { System.out.println("The stack is empty. No top element."); } } public void display() { if (top != null) { System.out.println("Stack elements (vertical):"); Node current = top; while (current != null) { System.out.println(current.data); current = current.next; } } else { System.out.println("The stack is empty."); } } public static void main(String[] args) { StackLinkedList stack = new StackLinkedList(); Scanner scanner = new Scanner(System.in); int choice; do { System.out.println("\nStack Operations:"); System.out.println("1. Push"); System.out.println("2. Pop"); System.out.println("3. Top"); System.out.println("4. Display"); System.out.println("5. Exit"); System.out.print("Enter your choice: "); choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("Enter element to push: "); int item = scanner.nextInt(); stack.push(item); break; case 2: stack.pop(); break; case 3: stack.top(); break; case 4: stack.display(); break; case 5: System.out.println("Exiting program."); break; default: System.out.println("Invalid choice. Try again."); } } while (choice != 5); scanner.close(); } }