/* queue.c Implementation of a FIFO queue abstract data type. by: Steven Skiena begun: March 27, 2002 */ /* Copyright 2003 by Steven S. Skiena; all rights reserved. Permission is granted for use in non-commerical applications provided this copyright notice remains intact and unchanged. This program appears in my book: "Programming Challenges: The Programming Contest Training Manual" by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. See our website www.programming-challenges.com for additional information. This book can be ordered from Amazon.com at http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ */ #include "queue.h" #include "item.h" #include "bool.h" init_queue(queue *q) { q->first = 0; q->last = QUEUESIZE-1; q->count = 0; } enqueue(queue *q, item_type x) { if (q->count >= QUEUESIZE) printf("Warning: queue overflow enqueue x=%d\n",x); else { q->last = (q->last+1) % QUEUESIZE; q->q[ q->last ] = x; q->count = q->count + 1; } } item_type dequeue(queue *q) { item_type x; if (q->count <= 0) printf("Warning: empty queue dequeue.\n"); else { x = q->q[ q->first ]; q->first = (q->first+1) % QUEUESIZE; q->count = q->count - 1; } return(x); } item_type headq(queue *q) { return( q->q[ q->first ] ); } int empty_queue(queue *q) { if (q->count <= 0) return (TRUE); else return (FALSE); } print_queue(queue *q) { int i,j; i=q->first; while (i != q->last) { printf("%d ",q->q[i]); i = (i+1) % QUEUESIZE; } printf("%2d ",q->q[i]); printf("\n"); }