Advertisement
Nayeemzaman

stack

Apr 2nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include<stdio.h>
  2. #define SIZE 5      /* Size of Stack */
  3.  
  4. char s[SIZE][20];        /* Global declarations */
  5. int top = 0;       /* -1 indicates stack is empty */
  6.  
  7. void push(char *element) /* Function for PUSH operation */
  8. {
  9.     if(isFull())
  10.         printf("\n Stack is full!\n");
  11.     else {
  12.         ++top;
  13.         strcpy(s[top],element);
  14.     }
  15. }
  16.  
  17. char* pop()           /* Function for POP operation */
  18. {
  19.     char element[20];
  20.     if(isEmpty()) {
  21.         printf("\nStack is Empty!\n");
  22.     }
  23.     else {
  24.         printf("\n Popped Element is %s \n", s[top]);
  25.         top--;
  26.     }
  27. }
  28.  
  29. int isFull()       /* Function to Check if Stack is Full */
  30. {
  31.     if(top == SIZE)
  32.         return 1;
  33.     return 0;
  34. }
  35.  
  36. int isEmpty()      /* Function to Check if Stack is Empty */
  37. {
  38.     if(top == 0)
  39.         return 1;
  40.     return 0;
  41. }
  42.  
  43. void display()  /* Function to display status of Stack */
  44. {
  45.     int i;
  46.     if(isEmpty())
  47.         printf(" \n Empty Stack\n");
  48.     else
  49.     {
  50.         for (i = top; i>=1; i--)
  51.             printf("%s\n", s[i]);
  52.     }
  53. }
  54.  
  55. void main()  /* Main Program */
  56. {
  57.     int opn;
  58.     char element[20];
  59.     do
  60.     {
  61.         printf("\n Stack Operations \n");
  62.         printf("\n Press 1-Push, 2-Pop,3-Display,4-Exit\n");
  63.         printf("\n Your option ? ");
  64.         scanf("%d", &opn);
  65.  
  66.         switch (opn)
  67.         {
  68.         case 1:
  69.             printf("\n Enter the element to be pushed ?");
  70.             scanf(" %[^\n]s", &element);
  71.             push(element);
  72.             break;
  73.         case 2:
  74.             pop();
  75.             break;
  76.         case 3:
  77.             printf("\n Status of Stack \n");
  78.             display();
  79.             break;
  80.         case 4:
  81.             printf("\n Terminating \n");
  82.             break;
  83.         default:
  84.             printf("\n Invalid Option !!! Try Again !! \n");
  85.             break;
  86.         }
  87.         printf("\n Press a Key to Continue . . . ");
  88.     }
  89.     while (opn != 4);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement