queue.c 871 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "queue.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. queue *initialize_queue()
  5. {
  6. queue *q;
  7. q = (queue *) malloc(sizeof(queue *));
  8. return q;
  9. }
  10. void enqueue(queue * q, int value)
  11. {
  12. struct node* aux;
  13. if (q->head == NULL) {
  14. q->head = (struct node *) malloc(sizeof(struct node *));
  15. q->head->next = NULL;
  16. q->head->value = value;
  17. q->tail = q->head;
  18. return;
  19. } else {
  20. aux = (struct node *) malloc(sizeof(struct node *));
  21. aux->next = NULL;
  22. aux->value = value;
  23. q->tail->next = aux;
  24. q->tail = aux;
  25. }
  26. }
  27. int dequeue(queue * q)
  28. {
  29. struct node *aux;
  30. int ret_val;
  31. ret_val = q->head->value;
  32. aux = q->head;
  33. q->head = q->head->next;
  34. free(aux);
  35. return (ret_val);
  36. }
  37. void destroy_queue(queue * q)
  38. {
  39. struct node *aux, *next;
  40. aux = q->head;
  41. while (aux != NULL) {
  42. next = aux->next;
  43. free(aux);
  44. if (next != NULL)
  45. aux = next;
  46. }
  47. free(q);
  48. }