Subscribe Us

Responsive Advertisement

Advertisement

//problem26 stack implementation using array

 

//problem26 stack implementation using array


#include<stdio.h>

#define size 10

stack[size];

int top=-1;

void push(int x)

{

    top++;

    if(top>=size)printf("overflow");

    else

    {

        stack[top]=x;

        printf("push\n");


    }

}

int peek()

{

    if(top>=0)

    {

        return stack[top];

    }

    else

    {

        return -1;

    }

}

int pop()

{

    if(top>=0)

    {

        int val=stack[top];

        top--;

        return val;

    }

    else

    {

        return -1;

    }

}

int main()

{


    push(10);

    push(20);

    push(30);

    push(40);

    printf("%d ",peek());

   printf("%d ",pop());

   printf("%d ",peek());

}


Post a Comment

0 Comments