Advertisement
Sheyshya

Pop and push

Dec 15th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<Conio.h>
  4. #define max 10
  5. struct stack
  6. {
  7. int top;
  8. int item[max];
  9.  
  10. }s;
  11.  
  12. void push();
  13. void pop();
  14. void display();
  15. void main()
  16. {
  17.       system("cls");
  18.       s.top=-1;
  19.  
  20.       int choice;
  21.       while (1)
  22.       {
  23.  
  24.             printf("Enter your choice");
  25.  
  26.             printf("\n1.Push\n 2. Pop\n 3.Display \n4.Exit\n");
  27.     scanf("%d",&choice);
  28.  
  29.             switch (choice)
  30.             {
  31.                   case 1:push();
  32.                   break;
  33.                   case 2:pop();
  34.                   break;
  35.                   case 3:display();
  36.                   break;
  37.                   case 4:
  38.                   exit(0);
  39.                   default: printf("Enter number in range: \n");
  40.             }
  41.       }
  42. }
  43.       void push()
  44.       {
  45.  
  46. int val;
  47.       if (s.top==max-1)
  48.       printf("Overflow:");
  49.       else
  50.       {
  51.         printf("Enter the element you want to push: \n");
  52.         scanf("%d",&val) ;
  53.         s.top=s.top+1;
  54.         s.item[s.top]=val;
  55.       }
  56.  
  57.  
  58.  
  59.  
  60.         }
  61.         void pop()
  62.         {
  63.  
  64.            if (s.top==-1)
  65.            {
  66.                  printf("Empty stack");
  67.            }
  68.            else
  69.            {
  70.                  printf("Deleted stack is %d\n",s.item[s.top]);
  71.                  s.top=s.top-1;
  72.            }
  73.  
  74.         }
  75.         void display()
  76.         {
  77.                 system("cls");
  78.               int i;
  79.               if (s.top==-1)
  80.               {
  81.                     printf("Empty\n");
  82.  
  83.               }
  84.               else
  85.               {
  86.                     for (i=s.top;i>=0;--i)
  87.                     {
  88.                         printf("Stack:\%d \n",s.item[i]);
  89.  
  90.                     }
  91.               }
  92.  
  93.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement