Untitled

 avatar
unknown
plain_text
5 months ago
1.3 kB
2
Indexable
#include <stdio.h>

#define MAXSTACK 5

int top = -1;
int stack[MAXSTACK];

void push() {
    if (top == MAXSTACK - 1) {
        printf("OVERFLOW\n");
    } else {
        int x;
        printf("Enter a value to push: ");
        scanf("%d", &x);
        stack[++top] = x;
        printf("Pushed: %d\n", x);
    }
}

void pop() {
    if (top == -1) {
        printf("UNDERFLOW\n");
    } else {
        printf("Popped: %d\n", stack[top]);
        top--;
    }
}

void display() {
    if (top == -1) {
        printf("Stack is Empty\n");
    } else {
        printf("Elements: ");
        for (int i = 0; i <= top; i++) {
            printf("%d ", stack[i]);
        }
        printf("\n");
    }
}

int main() {
    int choice;
    do {
        printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                push();
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                printf("Exiting - - - - -\n");
                break;
            default:
                printf("Invalid choice! Please try again.\n");
        }
    } while (choice != 4);

    return 0;
}
Editor is loading...
Leave a Comment