Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
1.5 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 10

int top = -1, inp_array[SIZE];
void push();
void pop();
void show();

int main(){
    int choice;
    
    while (true){
        printf("\n Perform operations on the stack: ");
        printf("\n1. Push the element.\n2. Pop the element.\n3. Show the stack.\n 4. End");
        printf("\nEnter the choice: ");
        scanf("%d", &choice);
        
        switch (choice){
            case 1:
                push();
                break;
                
            case 2:
                pop();
                break;
                
            case 3:
                show();
                break;
                
            case 4:
                exit(0);
                break;
                
            default:
                printf("\nInvalid choice.");
        }
    }
}

void push(){
    int x;
    
    if (top == SIZE - 1){
        printf("\nOverflow!!");
    } else {
        printf("\nEnter the element to be added onto the stack: ");
        scanf("%d", &x);
        top = top + 1;
        inp_array[top] = x;
    }
}

void pop(){
    if (top == -1){
        printf("\nUnderflow!!");
    } else {
        printf("\nPopped element :%d", inp_array[top]);
        top = top - 1;
    }
}

void show(){
    int i;
    if (top == -1){
        printf("\nUnderflow!!");
    } else {
        printf("\nElements present in the stack: \n");
        for (int i = top; i>= 0; --i){
            printf("%d\n", inp_array[i]);
        }
    }
}
Leave a Comment