Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
1
Indexable
Never
#include<stdio.h>
int main()
{
    int item,top=-1;
    printf("enter your stack size:");
    int size;
    scanf("%d",&size);
    int stack[size];


    while(1)
    {
printf("operations: ");
    printf("1.push\n");
    printf("2.pop\n");
    printf("3.display\n");
    printf("4.exit\n");

        int choice;
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:
            if(top>=size-1)
            {
                printf("stack overflow");
                break;
            }

else{
            printf("enter the element to push; \n");
            scanf("%d",&item);
            stack[++top]=item;
            printf("pushed %d item into the stack\n: ", item);
}
break;
        case 2:
            if(top==-1)
            {
                printf("stack underflow");
                break;
            }
else{

            stack [top--]=item;
            printf("poped %d item into the stack\n: ", item);
}
break;
        case 3:
            printf("display: ");
            for(int i=top; i>=0; i--)
            {
                printf("%d \n",stack[i]);
            }
            break;

        case 4:
            exit(0);
            printf("please enter correct value");

        }

    }



    return 0;
}