Not a member of GistPad yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define SIZE 5
- int stack[SIZE];
- int top = -1;
- // Function to push element
- void push() {
- int value;
- if (top == SIZE - 1) {
- cout << "Stack Overflow!" << endl;
- } else {
- cout << "Enter value to push: ";
- cin >> value;
- top++;
- stack[top] = value;
- cout << "Element pushed successfully." << endl;
- }
- }
- // Function to pop element
- void pop() {
- if (top == -1) {
- cout << "Stack Underflow!" << endl;
- } else {
- cout << "Popped element: " << stack[top] << endl;
- top--;
- }
- }
- // Function to display stack
- void display() {
- if (top == -1) {
- cout << "Stack is empty." << endl;
- } else {
- cout << "Stack elements are: ";
- for (int i = top; i >= 0; i--) {
- cout << stack[i] << " ";
- }
- cout << endl;
- }
- }
- int main() {
- int choice;
- do {
- cout << "\n--- STACK OPERATIONS ---" << endl;
- cout << "1. Push" << endl;
- cout << "2. Pop" << endl;
- cout << "3. Display" << endl;
- cout << "4. Exit" << endl;
- cout << "Enter your choice: ";
- cin >> choice;
- switch (choice) {
- case 1: push(); break;
- case 2: pop(); break;
- case 3: display(); break;
- case 4: cout << "Exiting program."; break;
- default: cout << "Invalid choice!" << endl;
- }
- } while (choice != 4);
- return 0;
- }
RAW Paste Data
Copied
