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 buffer[MAX];
    int count;
    int head;
    int tail;
}temp;
void process_stack(int n) {
    // Your logic here
    char operation[10];
    int val=0;
    int stack[MAX];
    int count=0;
    for (int i = 0; i<n; i++)
    {
        scanf("%s",operation);
        
        if(strcmp(operation, "push")==0){
            scanf("%d",&val);
            if(count<MAX){
                stack[count] = val;
                count++;
            }else{
                printf("Stack Overflow\n");
            }
        }else if(strcmp(operation,"pop")==0){
            if(count>0){
                count--;
                printf("%d\n",stack[count]);
                
            }else{
                printf("Stack Underflow\n");
            }
        }
    }
}

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

    process_stack(n);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote