Arifin99

Stack

Mar 3rd, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int a[10],choice,i,n,top=-1,x;
  4.  
  5. void push()
  6. {
  7.     if(top>=n-1)
  8.     {
  9.         printf("Stack is overflow\n");
  10.     }
  11.     else
  12.     {
  13.         printf("Enter the data to add\n");
  14.         scanf("%d",&x);
  15.         top++;
  16.         a[top]=x;
  17.     }
  18. }
  19.  
  20. void pop()
  21. {
  22.     if(top==-1)
  23.     {
  24.         printf("the stack is underflow\n");
  25.     }
  26.     else
  27.     {
  28.         printf("The poped element is:%d",a[top]);
  29.         top--;
  30.     }
  31. }
  32.  
  33. void display()
  34. {
  35.     if(top>=0)
  36.     {
  37.         printf("Elements are there\n");
  38.         for(i=top; i>=0; i--)
  39.         {
  40.             printf("%d\n",a[i]);
  41.  
  42.         }
  43.         printf("Press the next choice\n",choice);
  44.     }
  45.     else
  46.     {
  47.         printf("There is no element\n");
  48.     }
  49. }
  50.  
  51. int main()
  52. {   printf("Enter the stack size:");
  53.     scanf("%d",&n);
  54.     do
  55.     {
  56.       printf("Enter choice:");
  57.       scanf("%d",&choice);
  58.        switch(choice)
  59.        {
  60.        case 1:
  61.         {
  62.             push();
  63.             break;
  64.         }
  65.        case 2:
  66.         {
  67.             pop();
  68.             break;
  69.         }
  70.        case 3:
  71.         {
  72.             display();
  73.             break;
  74.         }
  75.        case 4:
  76.         {
  77.             printf("Exit");
  78.             break;
  79.         }
  80.        default:
  81.         {
  82.             printf("Invalid");
  83.  
  84.  
  85.         }
  86.        }
  87.     }
  88.     while(choice!=4);
  89.     return 0;
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment