Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
0
Indexable
Never
#include <stdio.h>
int main()
{
    int size,item,top=-1,popItem;
    printf("Enter your stack size: ");
    scanf("%d",&size);
    int stack[size];
    while(1){
        printf("\nstack operations:\n1. push\n2.pop\n3.display\n4.exit\n");
        int choice;
        scanf("%d",&choice);
        switch(choice){
    case 1:
                if (top == size-1) {
                    printf("Stack overflow!\n");
                }
                else{
                printf("Enter the element to push: ");
                scanf("%d", &item);
                stack[++top] = item;
                printf("Pushed %d into the stack.\n", item);
                }
                break;
    case 2:
                if (top == -1) {
                printf("stack underflow!\n");
               }
               else{
                  popItem= stack[top];
            top--;
            printf("Popped %d \n", popItem);
               }

            break;
    case 3:
                if (top == -1) {
                    printf("Stack underflow!\n");

                }
                else{
                   printf("Elements are:\n");
                     for (int i = top; i >= 0; i--)
                    printf("%d ", stack[i]);
                }
                break;
    case 4:
        exit(0);
    default:
        printf("please enter correct value");
    }
}
 return 0;
}