stack using array : C program

 



Here is the implementation of Stack using Array in C :


Program code:

#include <stdio.h>

#define size 10

int stack[size];

int top=-1;


void push(int x)

{

    if(top>=size)

    {

        printf("\noverflow");

    }

    else

    {

        top++;

        stack[top]=x;

    }

}


void pop()

{

    if(top<=0)

    {

        printf("\nstack is empty.");

        return;

    }

    return stack[top--];

}


void display()

{

    int i;

    if(top<0)

    {

        printf("\nstack is empty");

        return;

    }

    printf("\nstack elements are : ");

    for(i=top;i>=0;i--)

    {

        printf("%d ",stack[i]);

    }

    printf("\n");

}


int main()

{

    int op,el,exit=1;

    do

    {

        printf("what action you want to take :\n");

        printf("1.push   2.pop   3.display   4.topvalueandsize   5.exit\n");

        scanf("%d",&op);

        switch(op)

        {

        case 1:

            printf("enter element to be pushed :");

            scanf("%d",&el);

            push(el);

            break;

        case 2:

            printf("poping stack(%d)\n",top);

            pop();

            printf("after poping: ");

            display();

            break;

        case 3:

            display();

            break;

        case 4:

            printf("top value is %d\n",top);

            printf("size of the stack is %d\n",top+1);

            break;

        case 5:

            exit=0;

            break;

        default:

            printf("enter valid key.\n");

            break;

        }

    }while(exit==1);

    return 0;

}

---------------------------------------------------------------------------------

output :




Comments

Popular posts from this blog

swapping first and last digits of a number :Python Program

infix to postfix expression converter in "C" language

Implementation of Stack with Singly Linked List : C Program