a_pramanik

Stack implementation with array

Jul 18th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int stack[100],choice,n,top,x,i;
  4. void push(void);
  5. void pop(void);
  6. void display(void);
  7. int main()
  8. {
  9.     //clrscr();
  10.     top=-1;
  11.     printf("\n Enter the size of STACK[MAX=100]:");
  12.     scanf("%d",&n);
  13.     printf("\n\t STACK OPERATIONS USING ARRAY");
  14.     printf("\n\t--------------------------------");
  15.     printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
  16.     do
  17.     {
  18.         printf("\n Enter the Choice:");
  19.         scanf("%d",&choice);
  20.         switch(choice)
  21.         {
  22.             case 1:
  23.             {
  24.                 push();
  25.                 break;
  26.             }
  27.             case 2:
  28.             {
  29.                 pop();
  30.                 break;
  31.             }
  32.             case 3:
  33.             {
  34.                 display();
  35.                 break;
  36.             }
  37.             case 4:
  38.             {
  39.                 printf("\n\t EXIT POINT ");
  40.                 break;
  41.             }
  42.             default:
  43.             {
  44.                 printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
  45.             }
  46.                  
  47.         }
  48.     }
  49.     while(choice!=4);
  50.     return 0;
  51. }
  52. void push()
  53. {
  54.     if(top>=n-1)
  55.     {
  56.         printf("\n\tSTACK is over flow");
  57.          
  58.     }
  59.     else
  60.     {
  61.         printf(" Enter a value to be pushed:");
  62.         scanf("%d",&x);
  63.         top++;
  64.         stack[top]=x;
  65.     }
  66. }
  67. void pop()
  68. {
  69.     if(top<=-1)
  70.     {
  71.         printf("\n\t Stack is under flow");
  72.     }
  73.     else
  74.     {
  75.         printf("\n\t The popped elements is %d",stack[top]);
  76.         top--;
  77.     }
  78. }
  79. void display()
  80. {
  81.     if(top>=0)
  82.     {
  83.         printf("\n The elements in STACK \n");
  84.         for(i=top; i>=0; i--)
  85.             printf("\n%d",stack[i]);
  86.         printf("\n Press Next Choice");
  87.     }
  88.     else
  89.     {
  90.         printf("\n The STACK is empty");
  91.     }
  92.    
  93. }
Add Comment
Please, Sign In to add comment