IMPLEMENTATION OF STACK

IMPLEMENTATION OF STACK
#include
#include
void push();
void pop();
void display();
int i,choice,stack[100],n,x,top;
void main()
{
    top=-1;
    printf("Enter the size of the sack: \n");
    scanf("%d",&n);
    printf("1:push\n2:pop\n3:display\n");
    do
    {
        printf("Select your choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        default:
            printf("Enter the valid choice\n");
        }
    }
    while(choice!=4);
}
void push()
{
    if(top>=n-1)
    {
        printf("stack is over flow\n");
        getch();
    }
    else
    {
        printf("Enter the element to be pushed:");
        scanf("%d",&x);
        top++;
        stack[top]=x;
    }
}
void pop()
{
    if(top<=-1)
    {
        printf("The stack is under flow\n");
    }
    else
    {
        printf("The popped element is %d\n",stack[top]);
        top--;
    }
}
void display()
{
    if(top>=0)
    {
        printf("the elements in the stack are:");
        for(i=top;i>=0;--i)
            printf("%d\n",stack[i]);
    }
    else
    {
     printf("the stack is empty\n");
    }
}

Post a Comment

0 Comments