Advertisement
Guest User

Aeiou

a guest
Oct 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef DEBUG
  5. #define DEBUG(...) printf(__VA_ARGS__)
  6. #endif
  7.  
  8. #define SIZE 8
  9.  
  10. struct queue_s {
  11. int *array;
  12. int head;
  13. int tail;
  14. } queue_init = { .head=0, .tail=0 };
  15. typedef struct queue_s queue_t;
  16.  
  17.  
  18. int enqueue(queue_t *queue, int val) {
  19. if ((queue->tail + 1) % SIZE == queue->head) return -1;
  20.  
  21. queue->array[queue->tail] = val;
  22. queue->tail = (queue->tail + 1) % SIZE;
  23.  
  24. return 0;
  25. }
  26.  
  27. int dequeue(queue_t *queue) {
  28. if (queue->head == queue->tail) return -1;
  29.  
  30. queue->head = (queue->head + 1) % SIZE;
  31.  
  32. return 0;
  33. }
  34.  
  35. int print(queue_t queue) {
  36. int i;
  37.  
  38. if (queue.tail==queue.head) return -1;
  39.  
  40. for (i=queue.head; i!=queue.tail; i=(i+1)%SIZE) {
  41. printf("%d ", queue.array[i]);
  42. }
  43. printf("\n");
  44. return 0;
  45. }
  46.  
  47.  
  48. int main() {
  49. int val, ret_val, menu_choice;
  50. char c;
  51. queue_t queue = queue_init;
  52.  
  53. queue.array = (int *)malloc(sizeof(int)*SIZE);
  54. setbuf(stdout, NULL);
  55. do {
  56. menu_choice = 0;
  57. DEBUG("\n1 enqueue\n2 dequeue\n3 ispis (print)\n4 izlaz\n");
  58. scanf("%d", &menu_choice);
  59. switch (menu_choice) {
  60. case 1:
  61. scanf("%d", &val);
  62. ret_val = enqueue(&queue, val);
  63. if(ret_val==-1) printf("Red je pun.\n");
  64. break;
  65. case 2:
  66. ret_val = dequeue(&queue);
  67. if(ret_val==-1) printf("Red je prazan.\n");
  68. break;
  69. case 3:
  70. ret_val = print(queue);
  71. if(ret_val==-1) printf("Red je prazan.\n");
  72. break;
  73. case 4:
  74. break;
  75. default:
  76. while((c = getchar()) != '\n' && c != EOF);
  77. }
  78. } while(menu_choice!=4);
  79. free(queue.array);
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement