This Java program demonstrates the implementation of a Stack data structure with basic operations: Push (add an element) and Pop (remove the top element). The program allows user interaction via a simple menu to perform stack operations and display the current stack contents.
- Add elements to the stack (Push)
- Remove the top element from the stack (Pop)
- Display all elements in the stack
- Handles stack overflow and underflow situations
- Open the project in NetBeans or any Java IDE.
- Make sure both
StackDemo.javaandMain.javaare in the same package. - Build the project.
- Run the
Mainclass. - Follow the menu prompts in the console to perform stack operations.
Enter stack size: 5
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Choose an option: 1
Enter a number to push: 10
10 pushed onto stack.
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Choose an option: 1
Enter a number to push: 20
20 pushed onto stack.
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Choose an option: 3
Stack elements: 10 20
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Choose an option: 2
20 popped from stack.
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Choose an option: 3
Stack elements: 10
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Choose an option: 4
Exiting program...
Data Structures and Algorithms
Maryam Narimi