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
typedef struct {
    int data[MAX];
    int top;
} Stack;
void process_stack(int n) {
    Stack s1;
    char cmd[20];
    int n1;
    s1.top=-1;
    for(int i=0;i<n;i++){
        scanf("%s",cmd);
        if(strcmp(cmd,"push")==0){
            scanf("%d",&n1);
        if(s1.top>=(MAX-1)){
            printf("Stack Overflow\n");
        }
        else
      s1.data[++(s1.top)]=n1;
        }
        else if(strcmp(cmd,"pop")==0){
         if(s1.top<0){
            printf("Stack Underflow\n");
         }
         else
         printf("%d\n",s1.data[(s1.top)--]);
        }
    }
}

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

    process_stack(n);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote