#include "queue.h"

struct queue *enqueue(struct queue *queue, struct expression_element* element) {
    struct queue *new_elt = calloc(1,sizeof(struct queue));

    new_elt->data = element;

    if(!queue)
        return new_elt;

    struct queue* current = queue;

    while(current->next)
        current = current->next;

    current->next = new_elt;

    return queue;
}

struct expression_element* dequeue(struct queue **queue) {
    if (!queue || !*queue)
        return NULL;
    
    struct queue* removed = *queue;
    struct expression_element* res = removed->data;

    *queue = removed->next;
    free(removed);

    return res;
}

void queue_destroy(struct queue **queue) {
    if(!queue)
        return;
    while (*queue) {
        struct expression_element* dequeued = dequeue(queue);
        free(dequeued);
    }
}