适配器queue---deque/List做空间配置器实现

用deque(双端链表)实现queue,
源代码:

#pragma once

#include<stdio.h>
#include<deque>
#include<iostream>
using namespace std;
//用双端队列做空间配置器来实现队列
template<class T,class Con = deque<T> >
class Queue
{
public:
    typedef size_t SizeType;
    typedef T ValueType;
public:
    Queue()
    {}

    void Push(const ValueType data)
    {
        _con.push_back(data);
    }

    void Pop()
    {
        _con.pop_front();
    }

    bool Empty()
    {
        return _con.empty();
    }

    SizeType Size()
    {
        return _con.size();
    }

    ValueType& Front()
    {
        return _con.front();
    }

    const ValueType& Front()const
    {
        return _con.front();
    }
    ValueType& Back()
    {
        return _con.back();
    }
    const ValueType& Back()const
    {
        return _con.Back();
    }

private:
    Con _con;
};

用List做空间配置器实现queue
源代码:

template<class T>
struct ListNode
{
    ListNode(const T& data = T())
        : _prev(0)
        , _next(0)
        , _data(data)
    {}

    ListNode<T>* _prev;
    ListNode<T>* _next;
    T _data;
};


template<class T, class Ref, class Ptr>
class __ListIterator__
{
    typedef __ListIterator__<T, T&, T*> Iterator;
    typedef __ListIterator__<T, const T&, const T*> ConstIterator;
    typedef __ListIterator__<T, Ref, Ptr> Self;
    typedef ListNode<T>* LinkType;
    typedef Ref Reference;
    typedef Ptr Pointer;

public:
    __ListIterator__(LinkType x = 0)
        :_node(x)
    {}

    __ListIterator__(const Iterator& x)
    {
        _node = x._node;
    }

    bool operator==(const Iterator& x)
    {
        return _node == x._node;
    }

    bool operator!=(const Iterator& x)
    {
        return _node != x._node;
    }

    Reference operator*()
    {
        return (*_node)._data;
    }

    Pointer operator->()
    {
        return &(operator*());
    }

    Self& operator++()
    {
        _node = _node->_next;
        return *this;
    }
    Self operator++(int)
    {
        Self pTemp(*this);
        _node = _node->_next;
        return pTemp;
    }
    Self& operator--()
    {
        _node = _node->_prev;
        return *this;
    }
    Self operator--(int)
    {
        Self pTemp(*this);
        _node = _node->_prev;
        return pTemp;
    }
public:
    LinkType _node;
};

//空间配置器List类模板
template<class T>
class List
{
public:
    typedef ListNode<T> Node;
    typedef T ValueType;
    typedef ValueType& Reference;
    typedef const ValueType& ConstReference;
    typedef ValueType* Pointer;
    typedef const ValueType* ConstPointer;
    typedef Node* LinkType;
    typedef size_t SizeType;
    typedef __ListIterator__<T, T&, T*> Iterator;
    typedef __ListIterator__<T, const T&, const T*> ConstIterator;

public:
    //构造函数
    List()
    {
        EmptyInit();
    }
    List(SizeType n, const T& data)
    {
        EmptyInit();
        for(size_t idx=0; idx<n; ++idx)
        {
            PushBack(data);
        }
    }

    //拷贝构造函数
    List(const List<T>& l)
    {
        EmptyInit();
        Iterator it = l._node->_next;
        for(SizeType idx=0; idx<l.Size(); ++idx)
        {
            PushBack(it._node->_data);
            it++;
        }
    }

    //析构函数
    ~List()
    {
        /*Iterator it = End(); --it; while(it!=_node) { delete it._node; --it; }*/
        Clear();

        delete _node;
    }
    //赋值运算符重载
    //List<T>& operator=(const List<T>& l)
    //{
    // List<T> pTempList(l);
    // if(this != &l)
    // {
    // Iterator it = --End();
    // while(it != _node)
    // {
    // it = Erase(it);
    // }
    // //Clear();//错误
    // delete _node;
    // _node = pTempList._node;
    // }
    // return *this;
    //}

    //////////////////////////////////////////////
    Iterator Begin()
    {
        return _node->_next;
    }
    ConstIterator Begin()const
    {
        return _node->_next;
    }
    Iterator End()
    {
        return _node;
    }
    ConstIterator End()const
    {
        return _node;
    }
    bool Empty()
    {
        return _node==_node->_next;
    }
    SizeType Size()const
    {
        Iterator it = _node->_next;
        SizeType count = 0;
        while(it != _node)
        {
            count = count+1;
            it++;
        }
        return count;
    }

    SizeType MaxSize()const
    {
        return SizeType(-1);
    }
    Reference Front()
    {
        return *(Begin());
    }
    ConstReference Front()const
    {
        return *(Begin());
    }
    Reference Back()
    {
        return *(--End());
    }
    ConstReference Back()const
    {
        return *(--End());
    }
    Iterator Insert(Iterator pos, const T& x = T())
    {
        LinkType temp = new Node(x);
        temp->_next = pos._node;
        temp->_prev = pos._node->_prev;
        pos._node->_prev->_next = temp;
        pos._node->_prev = temp;
        return temp;
    }
    void PushFront(const T& x)
    {
        Insert(Begin(),x);
    }

    void PushBack(const T& x)
    {
        Iterator it = End();
        Insert(it,x);
    }
    //任意位置删除
    Iterator Erase(Iterator pos)
    {
        Iterator pRemNode = pos._node->_next;
        pos._node->_prev->_next= pos._node->_next;
        pos._node->_next->_prev = pos._node->_prev;

        pos._node->_next = pos._node->_prev = NULL;
        delete pos ._node;
        return pRemNode;
    }
    void PopFront()
    {
        Erase(Begin());
    }
    void PopBack()
    {
        Erase(--End());
    }
    void ReSize(SizeType n, const T& data)
    {
        if(Size() >= n)
            {
                SizeType size1 = Size()-n; 
                while(size1--)
                {
                    PopBack();
                }
            }
            else
            {
                SizeType size2 = n-Size();
                while(size2--)
                {
                    PushBack(data);
                }
            }
    }
    void Assign(SizeType n, const T& data)
    {
        Iterator pTemp = _node->_next;
        if(Size()>=n)
        {
            for(size_t idx=0;idx<n; ++idx)
            {
                pTemp._node->_data = data;
                ++pTemp;
            }
            for(size_t idx = 0; idx<Size()-n; ++idx)
            {
                PopBack();
            }
        }
        else
        {
            pTemp = _node->_next;
            size_t size = Size();
            for(size_t idx=0; idx<size; ++idx)
            {
                pTemp._node->_data = data;
                ++pTemp;
            }
            for(size_t idx = 0;idx<n-size; ++idx)
            {
                PushBack(data);
            }
        }
    }
    void Clear()
    {
        Iterator it = --End();
        while(it != _node)
        {
            it = Erase(it);
        }
        _node->_next = _node;
        _node->_prev = _node;
    }
private:
    void EmptyInit()
    {
        _node = new Node;
        _node->_data = 0;
        _node->_next = _node ;
        _node->_prev = _node;
    }
protected:
    LinkType _node;
};
//Queue的模板类
template<class T,class Con = List<T> >
class Queue
{
public:
    typedef size_t SizeType;
    typedef T ValueType;
public:
    Queue()
    {}
    void Push(const ValueType data)
    {
        _con.PushBack(data);
    }
    void Pop()
    {
        _con.PopFront();
    }
    bool Empty()
    {
        return _con.Empty();
    }
    SizeType Size()const
    {
        return _con.Size();
    }
    ValueType& Front()
    {
        return _con.Front();
    }
    const ValueType& Front()const
    {
        return _con.Front();
    }
    ValueType& Back()
    {
        return _con.Back();
    }
    const ValueType& Back()const
    {
        return _con.Back();
    }
private:
    Con _con;
};

你可能感兴趣的:(Queue,适配器)