Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. //STACK PROGRAM
  2. /*
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #define SIZE 5
  6. //stack implemented using array, LIFO
  7.  
  8. //stack_init(): Make stack empty
  9. //empty(): return true if stack is empty, false if otherwise
  10. //push(val): add val to stack
  11. //pop(): remove top item
  12. //top(): return top item, do not remove
  13.  
  14. //input positive number will push to stack
  15. //input negative number will invoke top()
  16. //input 0 will invoke pop()
  17.  
  18. int stack[SIZE],t,input;
  19.  
  20. void stack_init(void);
  21. bool empty(void);
  22. bool push(int);
  23. void pop(void);
  24. int top(void);
  25. bool isFull(void);
  26.  
  27. int main(void) {
  28. printf("Hello World\n");
  29. stack_init();
  30.  
  31. while(1){
  32.  
  33. printf("input:");
  34. scanf("%d",&input);
  35. if(input>0){
  36. if(push(input)){
  37. //printf("pushed!\n");
  38. }else{
  39. printf("array is full!\n");
  40. }
  41. }else if(input<0){
  42. if(top()!=-1) printf("output: %d\n", top());
  43. else printf("Error - Stack is empty\n");
  44. }else pop();
  45. }
  46. return 0;
  47. }
  48.  
  49. void stack_init(){
  50. t = -1;
  51. }
  52.  
  53. bool empty(){
  54. if (t == -1) return true;
  55. else return false;
  56. }
  57.  
  58. bool push(int val){
  59. if(isFull()){
  60. return false;
  61. }else{
  62. t=t+1;
  63. stack[t]=val;
  64. return true;
  65. }
  66. }
  67.  
  68. void pop(){
  69. if(!empty()) t=t-1;
  70. else printf("Error - Stack is empty\n");
  71. }
  72.  
  73. int top(){
  74. if(!empty()) return stack[t];
  75. else return -1;
  76. }
  77.  
  78. bool isFull(){
  79. if (t==SIZE){
  80. return true;
  81. }else{
  82. return false;
  83. }
  84. }
  85. */
  86.  
  87.  
  88.  
  89. //QUEUE PROGRAM
  90. #include <stdio.h>
  91. #include <stdbool.h>
  92. #define SIZE 4
  93.  
  94. //queue_init():makes queue empty
  95. //empty:return true if queue is empty
  96. //enqueue(val):add val into queue
  97. //dequeue():remove first item FIFO
  98. //front(): return front item, do not remove
  99.  
  100. //input positive invoke enqueue
  101. //input negative invoke front
  102. //input 0 invoke dequeue
  103.  
  104. int data[SIZE],r,f,input;
  105.  
  106. void queue_init(void);
  107. bool empty(void);
  108. bool enqueue(int);
  109. void dequeue(void);
  110. int front(void);
  111. int rear(void);
  112.  
  113. int main(void) {
  114. printf("Hello world!\n");
  115. queue_init();
  116.  
  117. while(1){
  118. printf("input:");
  119. scanf("%d",&input);
  120. if(input>0){
  121. if(enqueue(input)){
  122. //printf("enqueued!\n");
  123. }else{
  124. printf("array is full!");
  125. }
  126.  
  127. }else if(input<0){
  128. if(front()==-42){
  129. printf("Error - Queue is empty\n");
  130. }else{
  131. printf("f:%d\n",front());
  132. printf("r:%d\n",rear());
  133. }
  134. }else{
  135. dequeue();
  136. }
  137. }
  138. return 0;
  139. }
  140.  
  141. void queue_init(){
  142. r = f = -1;
  143. }
  144.  
  145. bool empty(){
  146. if (r==-1 && f==-1){
  147. return true;
  148. }else{
  149. return false;
  150. }
  151. }
  152.  
  153. bool enqueue(int val){
  154. if(((r+1)==f)||((r==SIZE-1)&&(f==0))){
  155. return false;
  156. }else{
  157. if(empty()){
  158. r=0;
  159. f=0;
  160. }else{
  161. r=r+1;
  162. if(r==SIZE){
  163. r=0;
  164. }
  165. }
  166. //printf("f:%d,r:%d",f,r);
  167. data[r]=val;
  168. return true;
  169. }
  170. }
  171.  
  172. void dequeue(){
  173. if(!empty()){
  174. if(r==f){
  175. r=f=-1;
  176. }else{
  177. f=f+1;
  178. if(f==SIZE){
  179. f=0;
  180. }
  181. }
  182. }else{
  183. printf("Error - Queue is empty\n");
  184. }
  185. }
  186.  
  187. int front(){
  188. if(!empty()){
  189. return data[f];
  190. }else{
  191. return -42;
  192. }
  193. }
  194.  
  195. int rear(){
  196. if(!empty()){
  197. return data[r];
  198. }else{
  199. return -43;
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement