Queue is one of the commonly used data structures, can be easily implemented by the table, in order to understand the principle, we use the array to implement
1 /************************************************************************/2 /*the implementation of the queue, which can be implemented directly through the table, is implemented using an array3 /************************************************************************/4 5 #ifndef Queue_h6 7 #defineQueue_h8 9#include <assert.h>Ten One namespaceSTL A { -Template <typename object> - classQueue the { -Queue (intqueuesize) -: Front (0) -, Rear (1) +, Size (0) - , Capacity (queuesize) + { AArray =NewObject[queuesize]; at } - -~Queue () - { - assert (Array); - Delete[] Array; in } - to //Queue function + voidEnQueue (Object x) - { the if(Isfull ()) * { $ Throw();Panax Notoginseng } -array[rear++] =x; theRear = (++rear)%capacity; +++Size; A } the + //out Team function - voidDeQueue () $ { $ if(IsEmpty ()) - { - Throw(); the } -++Front;Wuyi--Size; the } - Wu BOOLisfull () - { About returnSize = =capacity; $ } - - BOOLIsEmpty () - { A returnSize = =0; + } the - Private: $object*Array; the intcapacity;//Maximum queue capacity the intFront;//Queue Header Position the intRear;//position at the end of the queue the intSize;//number of objects in the queue - }; in } the #endif // ! Queue_h
Queue--c++ Implementation