Simple cyclic queue (implementation of C ++ template Technology)

Source: Internet
Author: User

The following code is only used by me to review the data structure. The practicality of N is low ~~ Haha:>

/// Example of a simple cyclic queue implemented by C ++ template technology. /// # include <cstdlib> # include <iostream> # include <iomanip> # include <stdexcept> // cyclic queue template. //// because the end of the cyclic queue is always empty, the physical space of the queue should be 1 larger than the Logical space of the specified queue. // The returned queue size should be logical. //// when you enter or exit the queue, an exception is thrown when the top or bottom overflow occurs. // template <typename T> class circularqueue {PRIVATE: T * _ pqueue; size_t _ front, _ rear, _ size; static const size_t _ max_queue_size = 20; public: circularqueue (const size_t size = _ max_queue_size ):_ Front (0), _ rear (0), _ SIZE (size + 1) {_ pqueue = new T [_ SIZE];} ~ Circularqueue (void) {Delete [] _ pqueue;} size_t getsize (void) const {return _ size-1;} t getfront (void) const {If (isempty ()) {Throw STD: underflow_error ("the queue is empty! ");} Return _ pqueue [_ front];} bool isempty (void) const {return _ front ==_ rear;} bool isfull (void) const {return (_ rear + 1) % _ size = _ front;} void enqueue (const T & Val) {If (isfull () {Throw STD :: overflow_error ("the team is full! ");} _ Pqueue [_ rear] = val; _ rear = (_ rear + 1) % _ size;} t dequeue (void) {If (isempty ()) {Throw STD: underflow_error ("the team is empty! ");} T value = _ pqueue [_ front]; _ front = (_ front + 1) % _ size; Return Value ;}}; /// queue test // int main (void) {circularqueue <int> queue; const size_t queue_size = queue. getsize (); For (size_t I = 0; I <queue_size; ++ I) {queue. enqueue (I);} // queue. enqueue (queue_size); STD: cout <STD: Endl <"queue" <(queue. isfull ()? "": "No") <"full." <STD: Endl <"queue" <(queue. isempty ()? "": "No") <"is empty. "<STD: Endl; For (size_t I = 0; I <queue_size; ++ I) {STD: cout <STD: SETW (3) <queue. dequeue ();} // STD: cout <STD: SETW (3) <queue. dequeue (); STD: cout <STD: Endl <"queue" <(queue. isfull ()? "": "No") <"full." <STD: Endl <"queue" <(queue. isempty ()? "": "No") <"blank." <STD: Endl; return exit_success ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.