/ * Queue.h * / # iFNDef __queue_h __ # define __queue_h__
#include
Extern "C" {void exit (int);}
Const int ndefaultqueueesize = 50;
Template Class Queue {Private: T * Qlist; // Store the neck of the queue element (array) int size; // Queue size (capacity) int around; // Team first int; // (The next position of the last element) INT count; // The number of elements in the queue PUBLIC: // Constructor Queue (INT ITSIZE = NDEFAULTQUEESIZE) {IF (INITSIZE <1) INITSIZE = NDEFAULTQUEESIZE; Qlist = New T ]; If (! Qlist) {cerr << "storage space allocation failed, the application will terminate!" << endl; exit (1);} front = 0; rear = 0; count = 0; size = INITSIZE;} // Destructor ~ Queue () {if (qlist) delete [] qlist; front = 0; rear = 0; count = 0; size = 0;} // Judgment Queue is empty int rt qempty () {// Return Front == REAR; RETURN Count == 0;} // Judgment Queue is full INT Qfull () {// Return (Rear 1)% size == front; return count == size;} // queue length INT QLENGTH () {RETURN Count; // Return (Rear - Front Size)% size;} // Team Tail Insert (Addition) Element Void Qinsert (cons T T T & ITEM) {if (count == size) {CERR << "queue is full, no additional elements. << Endl; Return;} count ; Qlist [REAR] = item; rest = (rear 1)% size; // REAR always points to the next position of the last element} // Team 1 Delete Element T Qdelete (T & data) {if (count> 0) {data = Qlist [Front]; count -; front = (Front 1)% size; // front shift to the next position} Else Cerr << "queue has been empty Unable to continue deletion.
"<< endl; returnid;} // Read the lead element T QFront (t & data) {if (count> 0) DATA = Qlist [Front]; Else Cerr <<" team is empty, unable to read the team first Value of elements. << Endl; Return Data;} // Empty queue void clearqueue () {front = 0; rear = 0; count = 0;}}; # ENDIF / *! __ queue_h__ * /// queuetest.cpp # incrude "Queue .h "void main () {queue q; int E;
Q.Qinsert (100); cout << "Front: << Q.QFront (e) << Endl; Q.Qinsert (256); cout <<" length: "<< Q.QLENGTH () << ENDL Q.QDelete (); cout << "Length:" << q.qlength () << endl;}