123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #include "queue.h"
- #include <stdlib.h>
- #include <stdio.h>
- queue *initialize_queue()
- {
- queue *q;
- q = (queue *) malloc(sizeof(queue *));
- return q;
- }
- void enqueue(queue * q, int value)
- {
- struct node* aux;
- if (q->head == NULL) {
- q->head = (struct node *) malloc(sizeof(struct node *));
- q->head->next = NULL;
- q->head->value = value;
- q->tail = q->head;
- return;
- } else {
- aux = (struct node *) malloc(sizeof(struct node *));
- aux->next = NULL;
- aux->value = value;
- q->tail->next = aux;
- q->tail = aux;
- }
- }
- int dequeue(queue * q)
- {
- struct node *aux;
- int ret_val;
- ret_val = q->head->value;
- aux = q->head;
- q->head = q->head->next;
- free(aux);
- return (ret_val);
- }
- void destroy_queue(queue * q)
- {
- struct node *aux, *next;
- aux = q->head;
- while (aux != NULL) {
- next = aux->next;
- free(aux);
- if (next != NULL)
- aux = next;
- }
- free(q);
- }
|