Advertisement
Osher15151

hjg

May 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. void queue_init(queue *q)
  2. {
  3.     q->head = 0;
  4.     q->tail = -1;
  5.     q->count = 0;
  6. }
  7. void enqueue(queue *q, queue_item x)
  8. {
  9.     if (!queue_full(*q))
  10.     {
  11.         q->tail = (q->tail + 1) % QUEUE_MAX_SIZE;
  12.         q->items[q->tail] = x;
  13.         q->count++;
  14.     }
  15. }
  16. queue_item dequeue(queue *q)
  17. {
  18.     if (!queue_empty(*q))
  19.     {
  20.         queue_item x = q->items[q->head];
  21.         q->head = (q->head + 1) % QUEUE_MAX_SIZE;
  22.         q->count--;
  23.         return x;
  24.     }
  25. }
  26.  
  27.  
  28. queue_item queue_top(queue q)
  29. {
  30.     return q.items[q.head];
  31. }
  32.  
  33. int queue_full(queue q)
  34. {
  35.     return (q.count == QUEUE_MAX_SIZE);
  36. }
  37.  
  38. int queue_empty(queue q)
  39. {
  40.     return (q.count == 0);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement