1. #include <iostream>
  2. using namespace std;
  3. #define SIZE 5
  4. int stack[SIZE];
  5. int top = -1;
  6.  
  7. // Function to push element
  8. void push() {
  9. int value;
  10. if (top == SIZE - 1) {
  11. cout << "Stack Overflow!" << endl;
  12. } else {
  13. cout << "Enter value to push: ";
  14. cin >> value;
  15. top++;
  16. stack[top] = value;
  17. cout << "Element pushed successfully." << endl;
  18. }
  19. }
  20. // Function to pop element
  21. void pop() {
  22. if (top == -1) {
  23. cout << "Stack Underflow!" << endl;
  24. } else {
  25. cout << "Popped element: " << stack[top] << endl;
  26. top--;
  27. }
  28. }
  29. // Function to display stack
  30. void display() {
  31. if (top == -1) {
  32. cout << "Stack is empty." << endl;
  33. } else {
  34. cout << "Stack elements are: ";
  35. for (int i = top; i >= 0; i--) {
  36. cout << stack[i] << " ";
  37. }
  38. cout << endl;
  39. }
  40. }
  41. int main() {
  42. int choice;
  43. do {
  44. cout << "\n--- STACK OPERATIONS ---" << endl;
  45. cout << "1. Push" << endl;
  46. cout << "2. Pop" << endl;
  47. cout << "3. Display" << endl;
  48. cout << "4. Exit" << endl;
  49. cout << "Enter your choice: ";
  50. cin >> choice;
  51.  
  52. switch (choice) {
  53. case 1: push(); break;
  54. case 2: pop(); break;
  55. case 3: display(); break;
  56. case 4: cout << "Exiting program."; break;
  57. default: cout << "Invalid choice!" << endl;
  58. }
  59. } while (choice != 4);
  60.  
  61. return 0;
  62. }