A generic queue class, like the queue in C++stl

Source: Internet
Author: User

Template queue, the implementation of the queued, out team, query size and other functions. Implemented using a template.

#ifndef queue_hpp#define queue_hpp #include <assert.h> #include <stddef.h>template <typename t> class    Queue;template <typename t>class node{friend class queue<t>;    Public:node (T data = 0, Node *next = NULL):d ata_ (data), Next_ (next) {} private:t Data_; Node *next_;};        Template <typename t>class queue{public:typedef node<t> *p_node;        Queue ();        Queue (const queue &other);        Queue &operator = (const queue &other);        ~queue ();        void Clear ();        void Enqueue (T data);        void Dequeue (T &data);        BOOL IsEmpty ();    size_t size ();        Private:p_node Front_;        P_node Rear_; size_t Size_;}; Template <typename t>inline queue<t>::queue (): Front_ (null), Rear_ (null), Size_ (0) {}template <ty Pename t>inline queue<t>::queue (const Queue &other): Front_ (null), Rear_ (null), Size_ (0) {P_nod E p = Other.front_;        while (P! = NULL) {enqueue (P->data_);    p = p->next_;  }}template <typename t>inline queue<t> &queue<t>::operator = (const Queue &other) {if (This! =        &other) {clear ();        P_node p = other.front_;            while (P! = NULL) {enqueue (P->data_);        p = p->next_; }} return *this;} Template <typename t>inline Queue<t>::~queue () {clear ();}    Template <typename t>inline void Queue<t>::clear () {P_node p = front_, q = NULL;        while (P! = NULL) {q = p;        Delete p;    p = q->next_; } size_= 0;}     Template <typename t>inline void Queue<t>::enqueue (T data) {P_node NewNode = new node<t> (data, NULL);    if (IsEmpty ()) {Front_ = Rear_ = NewNode;        } else{rear_->next_ = NewNode;    Rear_ = rear_->next_; } size_++;} Template <typename t>inline void queue<t>::d equeue (T &datA) {assert (!isempty ());    P_node p = front_;    Front_ = front_->next_;    data = p->data_;    Delete p; size_--;} Template <typename t>inline bool Queue<t>::isempty () {return front_ = = NULL;} Template <typename t>inline size_t queue<t>::size () {return size_;} #endif/*queue_hpp*/


A generic queue class, like the queue in C++stl

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.