m2skills

STACK C ARR

Jun 11th, 2017
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /* program for stack implementation in c*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include<conio.h>
  5.  
  6. //global variables
  7. int stackk[20],top;
  8.  
  9. //function definition
  10. void push(int);
  11. int pop();
  12.  
  13. int main()
  14. {
  15.     int element,cont,choice,i;
  16.     top=-1;
  17.     cont=1;
  18.  
  19.     do
  20.     {
  21.         printf("\nTHE FOLLOWING CHOICES ARE AVAILIABLE : ");
  22.         printf("\n1.PUSH AN ELEMENT");
  23.         printf("\n2.POP AN ELEMENT");
  24.         printf("\n3.VIEW STACK CONTENTS");
  25.         printf("\nENTER YOUR CHOICE : ");
  26.         scanf("%d",&choice);
  27.  
  28.         switch(choice)
  29.         {
  30.             case 1:
  31.                 printf("\nENTER THE ELEMENT TO BE PUSHED : ");
  32.                 scanf("%d",&element);
  33.                 push(element);
  34.                 break;
  35.  
  36.             case 2:
  37.                 element=pop();
  38.                 if(element!=99999)
  39.                 {
  40.                     printf("\nTHE ELEMENT POPPED  IS : %d",element);
  41.                 }
  42.                 break;
  43.  
  44.             case 3:
  45.                 printf("\nTHE ELEMENTS OF STACK ARE : ");
  46.                 for(i=0;i<=top;i++)
  47.                 {
  48.                     printf("\n%d",stackk[i]);
  49.                 }
  50.         }
  51.         printf("\nDO YOU WANT TO CONTINUE (1/0): ");
  52.         scanf("%d",&cont);
  53.  
  54.     }while(cont==1);
  55.     return 0;
  56. }
  57.  
  58. void push(int element)
  59. {
  60.     //increment top value
  61.     top++;
  62.    
  63.     //check if stack is full
  64.     if(top==20)
  65.     {
  66.         printf("\nSTACKFULL!!!!!");
  67.     }
  68.     else
  69.     {
  70.         stackk[top]=element;
  71.         printf("\nELEMENT PUSHED!!");
  72.     }
  73.     return;
  74. }
  75.  
  76. int pop()
  77. {
  78.     int element;
  79.     //check if stack is empty
  80.     if(top<0)
  81.     {
  82.         printf("\nSTACKEMPTY!!!!!");
  83.         //consider all elements are smaller than 99999
  84.         return 99999;       //returning  99999 to indicate stackempty
  85.     }
  86.     else
  87.     {
  88.         element=stackk[top];
  89.         top--;
  90.         return element;
  91.     }
  92. }
Add Comment
Please, Sign In to add comment