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