About

A dynamically allocating queue of integers.

Code

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/*                                                                       */
/*  intQueue                                                             */
/*                                                                       */
/*  An implementation of a queue of integers.                            */
/*  Implemented functions:                                               */
/*      - create                                                         */
/*      - clear                                                          */
/*      - is empty                                                       */
/*      - size                                                           */
/*      - count                                                          */
/*      - enqueue                                                        */
/*      - dequeue                                                        */
/*      - print                                                          */
/*                                                                       */
/*************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
 
/* The intQueue 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 {
    int size;
    int *store;
    int front, rear;
} intQueue;
 
// Zero or one arguments for intQueueCreate_Internal.
#define intQueueCreate_0() intQueueCreate_Internal(1)
#define intQueueCreate_1(x) intQueueCreate_Internal(x)
#define _FUNC_OVERRIDE(_1, FUNC) FUNC
#define intQueueCreate(...) _FUNC_OVERRIDE(intQueueCreate_1(__VA_ARGS__), intQueueCreate_0())
#define intQueueIsEmpty(s) (q->rear == q->front)
#define intQueueSize(s) q->size
#define intQueueCount(s) q->rear - q->front
 
/*
 * Creates a new intQueue with a given size.
 */
intQueue* intQueueCreate_Internal(int size) {
    intQueue *q = (intQueue*)calloc(1, sizeof(intQueue));
    if ((q->store = (int*)calloc(size, sizeof(int))) == NULL)
        return NULL;
    q->size = size;
    q->front = 0;
    q->rear = 0;
    return q;
}
 
/*
 * Clears a intQueue and returns a pointer to a new empty queue.
 */
intQueue* intQueueClear(intQueue *q) {
    if (q != NULL)
        free(q);
    return intQueueCreate(1);
}
 
/*
 * Enqueues an item to the intQueue.
 */
void intQueueEnqueue(intQueue *q, int i) {
    if (q->rear > q->size - 1)
        q->store = (int*)realloc(q->store, ++q->size * sizeof(int));
    q->store[q->rear] = i;
    ++q->rear;
}
 
/*
 * Dequeues an element from the intQueue or returns NULL in case the
 * queue is empty.
 */
int intQueueDequeue(intQueue *q) {
    int i;
    if (intQueueIsEmpty(q))
        return (int) NULL;
    i = q->store[q->front];
    ++q->front;
    return i;
}
 
/*
 * Prints out the elements of the intQueue.
 */
void intQueuePrint(intQueue *q) {
    int i;
    if (intQueueIsEmpty(q)) {
        printf("Queue is empty.\n");
        return;
    }
    printf("Elements in the queue: ");
    i = q->front;
    do {
        printf("%d ", q->store[i]);
    } while (++i < q->rear);
    printf("\n");
}
 
int main(void) {
    intQueue *q = intQueueCreate(2);
    printf("queue size: %d\n", intQueueSize(q));
    intQueueEnqueue(q, 10);
    intQueueEnqueue(q, 1000);
    intQueueEnqueue(q, 6);
    intQueuePrint(q);
    printf("queue size: %d\n", intQueueSize(q));
    printf("number of elements in the queue: %d\n", intQueueCount(q));
    printf("Dequeue: %d\n", intQueueDequeue(q));
    printf("Dequeue: %d\n", intQueueDequeue(q));
    intQueuePrint(q);
    printf("Dequeue: %d\n", intQueueDequeue(q));
    intQueuePrint(q);
}