Untitled

 avatar
unknown
plain_text
a year ago
1.5 kB
14
Indexable
#include <stdio.h>
#define MAX 50

int arr[MAX];
int top = -1;

void push(int);
void pop();
void peep();

int main() {
    int d, ch;

    while (1) {
        printf("\n1: Push\n2: Pop\n3: Peep\n4: Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);

        switch (ch) {
            case 1:
                printf("Enter element to push: ");
                scanf("%d", &d);
                push(d);
                break;
            case 2:
                pop();
                break;
            case 3:
                peep();
                break;
            case 4:
                return 0;
            default:
                printf("Invalid choice. Try again.\n");
        }
    }

    return 0;
}

void push(int d) {
    if (top >= MAX - 1)
        printf("\n\t**** STACK OVERFLOW ****\n");
    else {
        top++;
        arr[top] = d;
    }
}

void pop() {
    if (top == -1)
        printf("\n\t**** STACK UNDERFLOW ****\n");
    else {
        printf("Popped element is -> %d\n", arr[top]);
        top--;
    }
}

void peep() {
    if (top == -1) {
        printf("\n\t**** STACK IS EMPTY ****\n");
        return;
    }

    printf("\nCurrent Stack (Top to Bottom):\n");
    for (int i = top; i >= 0; i--) {
        if (i == top)
            printf("%d <- TOP\n", arr[i]);
        else
            printf("%d\n", arr[i]);
    }
}
Editor is loading...
Leave a Comment