Simple chain 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 simple chain queue implemented by C ++ template technology. // # include <cstdlib> # include <iostream> # include <iomanip> # include <stdexcept> // pre-declaration of chained queue template. template <typename T> class queue; // chained queue node class template. // template <typename T> class node {friend class queue <t>; private: T _ value; node <t> * _ pnext; public: node (void ): _ pnext (null) {NULL;} node (const T & Val): _ value (VAL), _ pnext (null) {null ;}}; //// chained queue template. // use a one-way linked list with leading nodes and tail pointers. // When the queue is empty, the tail Pointer Points to the header node. // access the node from the first node when the node is in the queue. The node is disconnected from the last node when the node is in the queue. // template <typename T> class incluqueue {PRIVATE: node <t> * _ pfront, * _ prear; public: incluqueue (void );~ Incluqueue (void); bool isempty (void) const; size_t getlength (void) const; t getfront (void) const; void enqueue (const T & Val ); t dequeue (void) ;}; template <typename T> inline queue <t >:: queue Queue (void) {_pfront = new node <t> (); _ prear = _ pfront;} template <typename T> queue <t> ::~ Invalid Queue (void) {for (node <t> * pdel = _ pfront-> _ pnext; null! = Pdel; pdel = _ pfront-> _ pnext) {_ pfront-> _ pnext = pdel-> _ pnext; # If! Defined (ndebug) STD: cout <"delete node:" <pdel-> _ value <STD: Endl; # endifdelete pdel;} Delete _ pfront ;} template <typename T> inline bool incluqueue <t>: isempty (void) const {return _ pfront = _ prear;} template <typename T> size_t incluqueue <t> :: getlength (void) const {size_t Len = 0; For (node <t> * ptemp = _ pfront-> _ pnext; null! = Ptemp; ++ Len) {ptemp = ptemp-> _ pnext;} return Len;} template <typename T> inline t queue <t >:: getfront (void) const {return _ pfront-> _ pnext-> _ value;} template <typename T> inline void incluqueue <t >:: enqueue (const T & Val) {node <t> * pnew = new node <t> (VAL); _ prear-> _ pnext = pnew; _ prear = pnew ;} template <typename T> inline t queue <t >:: dequeue (void) {If (isempty () {Throw STD: underflow_error ("Team Overflow! ");} Node <t> * pdel = _ pfront-> _ pnext; _ pfront-> _ pnext = pdel-> _ pnext; t value = pdel-> _ value; delete pdel; If (null ==_pfront-> _ pnext) {_ prear = _ pfront;} return value ;}/// queue test // int main (void) {queue <int> queue; const size_t queue_size = 20; For (size_t I = 0; I <queue_size; ++ I) {queue. enqueue (I);} STD: cout <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. 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.