Subscribe Us

Responsive Advertisement

Advertisement

stack implementation in c

 

stack implementation  in c



#include<stdio.h>

#define size 3

int stack[size];

int top=-1;

void push(int val)

{

    if(top<size-1)

    {

        top++;

        stack[top]=val;

        printf("Added value successfully\n");

    }

    else printf("No space\n");

}

int peek()

{

    if(top>=0)

    {

        return stack[top];

    }

    else {

        printf("No value\n");

        return -1;

    }

}

int pop()

{

    if(top>=0)

    {

        int x=stack[top];

        top=top-1;

        return x;



    }

     else {

        printf("No value\n");

        return -1;

    }


}

int main()

{

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

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

    push(5);

    push(4);

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

    int r=pop();

    printf("%d\n",r);

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

    


}


Post a Comment

0 Comments