//Copyright (c) 1996 A. Michael Berman. All Rights Reserved. //This code is from "Data Structures via C++: Objects by Evolution", to be //published by Oxford University Press USA. Permission is hearby granted to //use this code for any educational, non-commercial purpose, provided this //notice remains intact. //A. Michael Berman, Rowan College of New Jersey, berman@rowan.edu // cx9-3.cpp // Code Example 9-3: Implementation of a Circular Queue #include "cx9-2.h" int nextPos(int p) { if (p == maxQueue - 1) // at end of circle return 0; else return p+1; } template < class queueElementType > Queue < queueElementType >::Queue() { // start both front and rear at 0 f = 0; r = 0; } template < class queueElementType > void Queue < queueElementType >::enqueue(queueElementType e) { // add e to the rear of the queue, advancing r to next position assert(nextPos(r) != f); r = nextPos(r); elements[r] = e; } template < class queueElementType > queueElementType Queue < queueElementType >::dequeue() { // advance front of queue, return value of element at the front assert(f != r); f = nextPos(f); return elements[f]; } template < class queueElementType > queueElementType Queue < queueElementType >::front() { // return value of element at the front assert(f != r); return elements[nextPos(f)]; } template < class queueElementType > bool Queue < queueElementType >::isEmpty() { // return true if the queue is empty, that is, // if front is the same as rear return f == r; }