view paper/src/singleLinkedQueue.cbc @ 3:86340b0bf212

Update
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Wed, 17 Jan 2018 08:48:10 +0900
parents
children 10cb71d30fc0
line wrap: on
line source

#interface "Queue.h"

Queue* createSingleLinkedQueue(struct Context* context) {
    struct Queue* queue = new Queue(); // Allocate Queue interface
    struct SingleLinkedQueue* singleLinkedQueue = new SingleLinkedQueue(); // Allocate Queue implement
    queue->queue = (union Data*)singleLinkedQueue;
    singleLinkedQueue->top  = new Element();
    singleLinkedQueue->last = singleLinkedQueue->top;
    queue->clear = C_clearSingleLinkedQueue;
    queue->put  = C_putSingleLinkedQueue;
    queue->take  = C_takeSingleLinkedQueue;
    queue->isEmpty = C_isEmptySingleLinkedQueue;
    return queue;
}

__code clearSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...)) {
    queue->top = NULL;
    goto next(...);
}

__code putSingleLinkedQueue(struct SingleLinkedQueue* queue, union Data* data, __code next(...)) {
    Element* element = new Element();
    element->data = data;
    element->next = NULL;
    queue->last->next  = element;
    queue->last = element;
    goto next(...);
}

__code takeSingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(union Data* data, ...)) {
    struct Element* top = queue->top;
    struct Element* nextElement = top->next;
    if (queue->top == queue->last) {
        data = NULL;
    } else {
        queue->top = nextElement;
        data = nextElement->data;
    }
    goto next(data, ...);
}

__code isEmptySingleLinkedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) {
    if (queue->top == queue->last)
        goto whenEmpty(...);
    else
        goto next(...);
}