Description
Complete the following Queue class implementation: (Note that the array implementation should be an array of loops )
Enum ErrorCode
{
Success
Underflow,
Overflow
};
const int maxqueue = 100;
Template <class queueentry>
Class Myqueue
{
Public
Myqueue ();
BOOL empty () const; //Determine if the queue is empty
ErrorCode Append (const queueentry &item); //Queue Operation
ErrorCode serve (); //Out of team Operation
ErrorCode Retrieve (Queueentry &item) const; //Get team head element
BOOL Full () const;   //Determine if the queue is full
int size () const;   //Gets the number of elements in a queue
void Clear (); //Clear Queue all elements
ErrorCode Retrieve_and_serve (Queueentry &item); //Get team head elements and out of the team
Private
int front; Team Head Subscript
int rear; End of Team subscript
Queueentry entry[100]; Queue container
};
problem Source: Class 5, second week
Problem Solving Ideas:
Implementing a looping array
Circular arrays (loop array)
Imagine an array as a circular, rather than a non-linear; record team head and tail position with two subscripts front and rear; When you add an element, rear right shifts the element to the rear position. When Rear equals max (last index), rear 0. When the element is out of the queue, delete the element at the front position, and then front to the right. When front equals Max, the front is 0.
Boundary conditions
Issue: Unable to distinguish between full and empty queues.
Workaround:
1. A position in the array in the hollow;
2. Use a Boolean quantity to indicate whether the queue is full. This flag is true when rear is just before the front.
3. Use a counter (counter) to record the number of elements in the queue.
Here the code takes the first workaround:
The 1.front always points to the first element of the queue, which always has something on it;
Rear always points to the next position in the queue, which is always empty;
2. Assuming that the array is open to 100, then the queue is full, the actual length is 99, the judge is full: (rear + 1)% = = Front
Judgment null: Front==rear
3. When queued to the tail of the array, jump to the head to continue the loop.
Implementation code:
enumErrorCode {success, underflow, overflow};Const intMaxqueue = -; template<classQueueentry>classmyqueue{ Public: Myqueue (); BOOLEmpty ()Const; ErrorCode Append (ConstQueueentry &item);         ErrorCode serve (); ErrorCode Retrieve (Queueentry&item)Const; BOOLFull ()Const; intSize ()Const; voidClear (); ErrorCode Retrieve_and_serve (Queueentry&item);Private:         intFront; intRear; Queueentry entry[ -]; };template<classQueueentry>Myqueue<queueentry>::myqueue () {//InitializeFront = Rear =0;} Template<classQueueentry>BOOLMyqueue<queueentry>:: Empty ()Const{//determine if the queue is empty     if(front = = rear)return true; Else return false; } Template<classQueueentry>ErrorCode myqueue<queueentry>::append (ConstQueueentry &item) {//Queue    if(Full ()) {returnoverflow; } Else{Entry[rear]=item; Rear++; if(Rear = = -) Rear =0;//continue looping from the head of the array        returnsuccess; }}template<classQueueentry>ErrorCode myqueue<queueentry>::serve () {//out Team    if(Empty ()) {returnunderflow; } Else{Front++; if(Front = = -) Front =0; returnsuccess; }}template<classQueueentry>ErrorCode myqueue<queueentry>::retrieve (Queueentry &item)Const{//take the team head .     if(Empty ()) {returnunderflow; } Else{Item=Entry[front]; returnsuccess; }}template<classQueueentry>BOOLMyqueue<queueentry>::full ()Const{//determine if the queue is full     if((Rear +1) % -= = Front)return true; Else return false;} Template<classQueueentry>intMyqueue<queueentry>::size ()Const{//Judging the length of the team    if(Front <=rear) {        return(Rear-front); } Else {      return( --Front +rear); }}template<classQueueentry>voidMyqueue<queueentry>::clear () {//Clear QueueRear = Front =0;} Template<classQueueentry>ErrorCode myqueue<queueentry>::retrieve_and_serve (Queueentry &item) {//take the team head and get out of the teamErrorCode Flag; Flag=retrieve (item);    Serve (); returnFlag;} 
(Ben Boven more or less reference other online materials, but the time has long forgotten the original reference, here to thank them! )
The implementation of "queue" based on loop array