view Paper/src/SingleLinkedQueue.cbc @ 88:8c1735151e2a default tip

...
author matac42 <matac@cr.ie.u-ryukyu.ac.jp>
date Wed, 28 Feb 2024 15:39:29 +0900
parents 905910e9fb04
children
line wrap: on
line source

#include "context.h"
#include <stdio.h>
#impl "Queue.h" as "SingleLinkedQueue.h"
#data "Node.h"
#data "Element.h"

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

//  ~~省略~~

__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, ...)) {
    printf("take\n");
    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, ...);
}

// ~~省略~~