Push and pop in stack

 #include <stdio.h>

#include <stdlib.h>


#define MAX 100


int stack[MAX];

int top = -1;


void push(int value) {

    if (top >= MAX - 1) {

        printf("Stack Overflow\n");

        return;

    }

    stack[++top] = value;

}


int pop() {

    if (top < 0) {

        printf("Stack Underflow\n");

        return -1;

    }

    return stack[top--];

}


int main() {

    push(10);

    push(20);

    push(30);


    printf("%d popped from stack\n", pop());

    printf("%d popped from stack\n", pop());

    printf("%d popped from stack\n", pop());


    return 0;

}

KRISHNA

Author & Editor

Thanks for visiting xmonocodes , i hope you are getting everything you want.

0 comments:

Post a Comment