//queueTest.c //John T. Rine //October 29, 2011 #include #include struct Queue { char *queueArray; int count; int size; int startIndex; }; typedef struct Queue queue; void createQueue(queue *, int); void destroyQueue(queue *); void enqueue(queue *, char); void printQueue(queue *); int queueCount(queue *); char dequeue(queue *); int main(int argc, char **argv) { queue newQueue; createQueue(&newQueue, 25); destroyQueue(&newQueue); enqueue(&newQueue, 'J'); enqueue(&newQueue, 'o'); enqueue(&newQueue, 'h'); enqueue(&newQueue, 'n'); printf("Calling printQueue(&newQueue)...\n"); printQueue(&newQueue); printf("Calling and printing return value from dequeue...\n"); printf("%c, ", dequeue(&newQueue)); printf("%c, ", dequeue(&newQueue)); printf("\n"); printf("Calling printQueue(&newQueue)...\n"); printQueue(&newQueue); return 0; } void createQueue(queue *queuePTR, int size) { char *queueArrayPTR = NULL; queueArrayPTR = (char *)malloc(sizeof(char) * size); queuePTR->queueArray = queueArrayPTR; queuePTR->size = size; queuePTR->count = 0; queuePTR->startIndex = 0; } void destroyQueue(queue *queuePTR) { free(queuePTR->queueArray); } void enqueue(queue *queuePTR, char data) { int queueArrayIndex; if(queuePTR->count < queuePTR->size) { queueArrayIndex = queuePTR->startIndex + queuePTR->count; queuePTR->queueArray[queueArrayIndex] = data; queuePTR->count++; } else printf("Queue is full!\n"); } void printQueue(queue *queuePTR) { int i; for(i = 0; i < queuePTR->count; i++) printf("%c, ",queuePTR->queueArray[(queuePTR->startIndex + i)%queuePTR->size]); printf("\n"); } int queueCount(queue *queuePTR) { return queuePTR->count; } char dequeue(queue *queuePTR) { char ret; if(queuePTR->count == 0) { printf("queue is empty!\n"); exit(1); } ret = queuePTR->queueArray[queuePTR->startIndex]; queuePTR->count--; queuePTR->startIndex = (queuePTR->startIndex + 1)%queuePTR->size; return ret; }