码迷,mamicode.com
首页 > 编程语言 > 详细

一个通用的队列类,和C++STL中的Queue一样

时间:2014-09-25 17:36:59      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:c++   队列模板   泛型编程   

模板队列,实现了入队,出队,查询大小等功能。用模板实现的。

#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*/


一个通用的队列类,和C++STL中的Queue一样

标签:c++   队列模板   泛型编程   

原文地址:http://blog.csdn.net/nyist327/article/details/39551133

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!