88. Implement Stack Using Array with Push and Pop Operations

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define MAX 10

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

void push(int val){
    if(i<0){
        printf("Stack Overflow\n");
        return;
    }
    //printf("%d",arr[i]);
    arr[i]=val;
    i = i-1;
}

void pop(){
    if(i==MAX-1){
        printf("Stack Underflow\n");
        return;
    }
    i = i+1;
    printf("%d\n",arr[i]);
    
}
void process_stack(int n) {
    char input[10];
    int val=0;
    for(int i=0;i<n;i++){
        scanf("%s",input);
        if(strcmp(input, "push")==0){
            scanf("%d",&val);
            push(val);
        }
        if(strcmp(input, "pop")==0){
            scanf("%d",&val);
            pop();
        }

    }
}

int main() {
    int n;
    scanf("%d", &n);
    getchar(); // Consume newline after number
    process_stack(n);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote