Circular queue – sequential storage

Time:2020-11-30

Array to achieve circular queue


Create three files: queuearray. H, queuearray. C, and queuearraytest. C


queueArray.h
#ifndef QUEUE_ARRAY_H_
#define QUEUE_ARRAY_H_

#ifndef PTOI
#define PTOI( p ) ((int32_t)(int64_t)(p))
#endif
#ifndef ITOP
#define ITOP( i ) ((void *)(int64_t)(i))
#endif

#define ADT QueueArray

//Function: the process of comparison between a and B
//Parameters: A, B
//Return: a > B returns a positive number, a

queueArray.c
#include 
#include 
#include 
#include "queueArray.h"


//Function: print the error message and exit the program
//Parameters: expression (error judgment expression), message (error message to be printed)
//Return: none
//Note: only triggered when expression is true
#define ERROR_EXIT( expression, message )                                    \
if( (expression) ) {                                                         \
	fprintf( stderr, "\nerror location: file = %s, func = %s, line = %d.\n", \
	                       __FILE__, __func__, __LINE__ );                   \
	fprintf( stderr, "error  message: %s%s.\n\a",                            \
	                       (message) != NULL ? (message) : __func__,         \
		                   (message) != NULL ? "" : " function error" );     \
	exit( EXIT_FAILURE );                                                    \
}

#define ADT QueueArray


struct QueueArray {
	int32_t capacity;
	int32_t size;
	int32_t head;
	void *array[0];
};

ADT *newQueueArray( int32_t capacity ) {
	ADT *queue = NULL;

	capacity = capacity < 0 ? 512 : capacity;
	queue = malloc( sizeof(*queue) + sizeof(queue->array[0]) * capacity );
	ERROR_EXIT( !queue, NULL );
	queue->capacity = capacity;
	queue->size = 0;
	queue->head = 0;

	return queue;
}

void *addQueueArray( QueueArray *queue, void *data ) {
	ERROR_EXIT( !queue || queue->size >= queue->capacity, NULL );
	queue->array[(queue->head + queue->size++) % queue->capacity] = data;

	return data;
}

void *pollQueueArray( QueueArray *queue ) {
	void *data = NULL;

	ERROR_EXIT( !queue || queue->size < 1, NULL );
	data = queue->array[queue->head];
	queue->head = (queue->head + 1) % queue->capacity;
	--queue->size;

	return data;
}

void *peekQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue || queue->size < 1, NULL );

	return queue->array[queue->head];
}

void *peekTailQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue || queue->size < 1, NULL );

	return queue->array[(queue->head + queue->size - 1) % queue->capacity];
}

int existQueueArray( QueueArray *queue, void *data, CompareFunc *cmp ) {
	ERROR_EXIT( !queue || !cmp, NULL );

	for( int32_t i = 0; i < queue->size; ++i ) {
		if( !cmp( queue->array[(queue->head + i) % queue->capacity], data ) ) {
			return 1;
		}
	}

	return 0;
}

int32_t findQueueArray( QueueArray *queue, void *data, CompareFunc *cmp ) {
	int32_t i = 0, j = 0;

	ERROR_EXIT( !queue || !cmp, NULL );
	for( i = 0; i < queue->size; ++i ) {
		j = (queue->head + i) % queue->capacity;
		if( !cmp( queue->array[j], data ) ) {
			return j;
		}
	}

	return -1;
}

int32_t findTailQueueArray( QueueArray *queue, void *data, CompareFunc *cmp ) {
	int32_t i = 0, j = 0;

	ERROR_EXIT( !queue || !cmp, NULL );
	for( i = queue->size - 1; i >= 0; --i ) {
		j = (queue->head + i) % queue->capacity;
		if( !cmp( queue->array[j], data ) ) {
			return j;
		}
	}

	return -1;
}

int32_t sizeQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue, NULL );

	return queue->size;
}


int emptyQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue, NULL );

	return queue->size < 1;
}

int fullQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue, NULL );

	return queue->size >= queue->capacity;
}

int32_t capacityQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue, NULL );

	return queue->capacity;
}

void clearQueueArray( QueueArray *queue ) {
	ERROR_EXIT( !queue, NULL );

	queue->size = 0;
	queue->head = 0;
}

void delQueueArray( QueueArray **queue ) {
	ERROR_EXIT( !queue, NULL );
	free( *queue );
	*queue = NULL;
}

queueArrayTest.c
#include 
#include 
#include 
#include 
#include "queueArray.h"


//A > B returns a positive number