Table of Contents

About

This implementation uses Amiga OS types and memory-allocation semantics in order to be used for other programs that rely on a queue implementation.

The program can be compiled on the Amiga using SAS/C by issuing:

sc link queues.c

When the program is run, it will perform a series of operations using the implemented queue functions and display the output in the calling shell:

queue size: 1
Elements in the queue: Good Day ! How are you? 
queue size: 4
number of elements in the queue: 4
Dequeue: Good
Dequeue: Day
Elements in the queue: ! How are you? 
Dequeue: !
Elements in the queue: How are you? 
Elements in the queue: How are you? Clear 
Dequeue: How are you?
Elements in the queue: Clear 
queue size: 1
Queue is empty.

Uses

Code

queues.c
/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/*                                                                       */
/*  stringQueue                                                          */
/*                                                                       */
/*  An implementation of a queue of strings for Amiga OS.                */
/*                                                                       */
/*  Implemented functions:                                               */
/*      - create                                                         */
/*      - clear                                                          */
/*      - is empty                                                       */
/*      - size                                                           */
/*      - count                                                          */
/*      - enqueue                                                        */
/*      - dequeue                                                        */
/*      - print                                                          */
/*                                                                       */
/*  Compile on Amiga OS using SAS/C and issuing: sc link queues.c        */
/*                                                                       */
/*************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include <proto/exec.h>
#include <proto/dos.h>
 
/* The stringQueue structure with front being the index of the front-mode
 * element and rear being the index of the last element in the queue
 */
typedef struct {
    ULONG size;
    UBYTE **store;
    ULONG front, rear;
} stringQueue;
 
/*
 * Creates a new stringQueue with a given size.
 */
stringQueue* stringQueueCreate(ULONG size) {
    stringQueue *q = (stringQueue*)AllocMem(
        sizeof(stringQueue), 
        MEMF_ANY|MEMF_CLEAR
    );
    if ((q->store = 
        (UBYTE**)AllocMem(
            size * sizeof(UBYTE **), 
            MEMF_ANY|MEMF_CLEAR
        )) == NULL) return NULL;
    q->size = size;
    q->front = 0;
    q->rear = 0;
    return q;
}
 
/*
 * Clears a stringQueue and returns a pointer to a new empty queue.
 */
stringQueue* stringQueueClear(stringQueue *q) {
    if (q != NULL)
        FreeMem(q, sizeof(q));
    return stringQueueCreate(1);
}
 
/*
 * Takes as parameter a stringQueue and returns 1 if the queue is empty 
 * or 0 if the queue is not empty.
 */
BOOL stringQueueIsEmpty(stringQueue *q) {
    return (BOOL)(q->rear == q->front);
}
 
/*
 * Returns allocated queue size (not the number of elements).
 */
ULONG stringQueueSize(stringQueue *q) {
    return q->size;
}
 
/*
 * Returns the number of elements in stringQueue.
 */
ULONG stringQueueCount(stringQueue *q) {
    return q->rear - q->front;
}
 
/*
 * Enqueues an element to the stringQueue.
 */
void stringQueueEnqueue(stringQueue *q, UBYTE *e) {
    UBYTE **qStore;
    if (q->rear > q->size - 1) { // realloc for AmigaOS
        qStore = (UBYTE **)AllocMem(
            (q->size + 1) * sizeof(UBYTE **), 
            MEMF_ANY|MEMF_CLEAR
        );
        CopyMem(q->store, qStore, q->size * sizeof(UBYTE **));
        FreeMem(q->store, sizeof(q->store));
        q->store = qStore;
        ++q->size;
    }
    q->store[q->rear] = (UBYTE*)AllocMem(
        strlen(e) * sizeof(UBYTE *) + 1, 
        MEMF_ANY|MEMF_CLEAR
    );
    CopyMem(e, q->store[q->rear], strlen(e) * sizeof(UBYTE *) + 1);
    ++q->rear;
}
 
/*
 * Dequeues an element from the stringQueue or returns NULL in case the
 * queue is empty.
 */
UBYTE *stringQueueDequeue(stringQueue *q) {
    UBYTE *e;
    if (stringQueueIsEmpty(q))
        return NULL;
    e = (UBYTE *)AllocMem(
        strlen(q->store[q->front]) * sizeof(UBYTE *) + 1, 
        MEMF_ANY|MEMF_CLEAR
    );
    CopyMem(
        q->store[q->front], 
        e, 
        strlen(q->store[q->front]) * sizeof(UBYTE *) + 1
    );
    ++q->front;
    return e;
}
 
/*
 * Prints out the elements of the stringQueue.
 */
void stringQueuePrint(stringQueue *q) {
    ULONG i;
    if (stringQueueIsEmpty(q)) {
        Printf("Queue is empty.\n");
        return;
    }
    Printf("Elements in the queue: ");
    i = q->front;
    do {
        Printf("%s ", q->store[i]);
    } while (++i < q->rear);
    Printf("\n");
}
 
int main(void) {
    stringQueue *q = stringQueueCreate(1);
    Printf("queue size: %ld\n", stringQueueSize(q));
    stringQueueEnqueue(q, "Good");
    stringQueueEnqueue(q, "Day");
    stringQueueEnqueue(q, "!");
    stringQueueEnqueue(q, "How are you?");
    stringQueuePrint(q);
    Printf("queue size: %ld\n", stringQueueSize(q));
    Printf("number of elements in the queue: %ld\n", stringQueueCount(q));
    Printf("Dequeue: %s\n", stringQueueDequeue(q));
    Printf("Dequeue: %s\n", stringQueueDequeue(q));
    stringQueuePrint(q);
    Printf("Dequeue: %s\n", stringQueueDequeue(q));
    stringQueuePrint(q);
    stringQueueEnqueue(q, "Clear");
    stringQueuePrint(q);
    Printf("Dequeue: %s\n", stringQueueDequeue(q));
    stringQueuePrint(q);
    q = stringQueueClear(q);
    Printf("queue size: %ld\n", stringQueueSize(q));
    stringQueuePrint(q);
}