Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #define CAPACITY 5
  5.  
  6. int menu (){
  7. int opt;
  8. printf("\n");
  9. printf(" STACK MENU \n");
  10. printf(" 0. PUSH \n");
  11. printf(" 1. POP \n");
  12. printf(" 2. Full \n");
  13. printf(" 3. Empty \n");
  14. printf(" 4. Print Stack \n");
  15. printf(" 5. Exit \n ");
  16. printf("\n");
  17. printf("Enter the option:");
  18. scanf("%d",&opt);
  19. return opt;
  20. }
  21.  
  22.  
  23. int arr[CAPACITY];
  24. int front =-1;
  25.  
  26. void init()
  27. {
  28. for(int i=0;i<CAPACITY;i++)
  29. {
  30. arr[i]=-1;
  31. }
  32. }
  33.  
  34. bool isFull()
  35. {
  36. if(front==CAPACITY-1)
  37. return true;
  38. else
  39. return false;
  40. }
  41.  
  42. bool isEmpty()
  43. {
  44. if(front==-1)
  45. return true;
  46. else
  47. return false;
  48. }
  49.  
  50. void push(int el)
  51. {
  52. if(isFull()==true)
  53. {
  54. printf("Stack is full\n");
  55. }
  56. else
  57. {
  58. front++;
  59. arr[front]=el;
  60. printf("New element %d was introduced in location %d\n",el,front);
  61. }
  62. }
  63.  
  64. int pop()
  65. {
  66. int del_el;
  67. if(isEmpty()==true)
  68. {
  69. return true;
  70. }
  71. else
  72. {
  73. del_el=arr[front];
  74. arr[front]=-1;
  75. front--;
  76. }
  77. return del_el;
  78. }
  79.  
  80. void print_stack()
  81. {
  82. for(int i=CAPACITY-1;i>=0;i--)
  83. {
  84. if(arr[i]!=-1)
  85. {
  86. printf("| %d |",arr[i]);
  87. }
  88. else {printf("| -- |");}
  89. printf("\n");
  90. printf("|_______| ");
  91. printf("\n");
  92. }
  93. }
  94.  
  95. int main()
  96. {
  97. int new,option;
  98. init();
  99. do{
  100. option = menu();
  101. switch(option){
  102. case 0:
  103. printf("Enter the element to introduce: ");
  104. scanf("%d",&new);
  105. push(new);
  106. break;
  107. case 1:
  108. printf("pop-->%d",pop());
  109. break;
  110.  
  111. case 2:
  112. if(isFull()==true)
  113. {
  114. printf("Stack is full\n");
  115. }
  116. else printf("Stacks is not full\n");
  117. break;
  118.  
  119. case 3:
  120. if(isEmpty()==true)
  121. {
  122. printf("Stack is empty\n");
  123. }
  124. else printf("Stacks is not empty\n");
  125. break;
  126. case 4:
  127. print_stack();
  128. break;
  129. case 5:
  130. printf("Program terminated\n");
  131. exit(0);
  132. break;
  133. }
  134. }while(option<=5);
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement