Template queue with copy constructor and value assignment operator

Source: Internet
Author: User
#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)            :data_(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 <typename T>inline Queue<T>::Queue(const Queue &other)    :front_(NULL),     rear_(NULL),     size_(0){    p_node 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>::dequeue(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*/

Template queue with copy constructor and value assignment operator

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.