Advertisement
niamulhasan

Stack At lab - 15-10-17

Oct 15th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdbool.h>
  4.  
  5. #define SIZE 5
  6.  
  7. // int stack[SIZE] = {1,2,3,4,5,6,7,8,9,10};
  8. int stack[SIZE-1];
  9. int top = -1;
  10.  
  11.  
  12. bool isEmpty()
  13. {
  14.     return stack[0] == NULL ? true : false;
  15. }
  16.  
  17. bool isFull()
  18. {
  19.     return top == SIZE-1 ? true : false;
  20. }
  21.  
  22. void push()
  23. {
  24.     int val;
  25.     if(isFull() == false)
  26.     {
  27.         printf("Enter Value: ");
  28.         scanf("%d", &val);
  29.         stack[top+1] = val;
  30.         top+=1;
  31.     }
  32.     else
  33.     {
  34.         printf("\n\n\nFull");
  35.     }
  36. }
  37.  
  38. void pop()
  39. {
  40.     if(!isEmpty()){
  41.         stack[top] = NULL;
  42.         top-=1;
  43.     }
  44. }
  45.  
  46. void peek()
  47. {
  48.     printf("\n Top peek element is %d\n", stack[top]);
  49. }
  50.  
  51. void display()
  52. {
  53.     int i=0;
  54.     printf("\n");
  55.     for(i=0; stack[i] != NULL; i++)
  56.     {
  57.         printf(" %d -", stack[i]);
  58.     }
  59.     printf("\b\b\b\b\n");
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65.     while(1)
  66.     {
  67.         int choice;
  68.         printf("MENU \n 1. Push \n 2. Pop \n 3. Peak \n 4. Exit \n");
  69.  
  70.         scanf("%d", &choice);
  71.  
  72.         if(choice == 1)
  73.         {
  74.             push();
  75.         }
  76.         else if(choice == 2)
  77.         {
  78.             pop();
  79.  
  80.         }
  81.         else if(choice == 3)
  82.         {
  83.             peek();
  84.         }
  85.         else if(choice == 4)
  86.         {
  87.             exit(1);
  88.         }
  89.         display();
  90.     }
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement