自定义的双端队列(deque)模板类

#include <iostream>

template <typename Object>
class deque{
private:
    struct Node{
        Object data;
        Node* prev;
        Node* next;

        Node(const Object& d=Object(), Node* p=nullptr, Node* n=nullptr):data(d),prev(p),next(n){}
    };
    int theSize;
    Node* head;
    Node* tail;

    void init()
    {
        theSize=0;
        head = new Node;
        tail = new Node;
        head->next=tail;
        tail->prev=head;
    }
public:
    deque()
    {
        init();
    }
    ~deque()
    {
        clear();
        delete head;
        delete tail;
    }
    deque(const deque& rhs)
    {
        init();
        Node* ptr=rhs.head->next;
        while(ptr!=rhs.tail)
        {
            inject(ptr->data);
            ptr=ptr->next;
        }
    }
    const deque& operator=(const deque& rhs)
    {
        if(this==&rhs)
            return *this;
        clear();
        Node* ptr =rhs.head->next;
        while(ptr!=rhs.tail)
        {
            inject(ptr->data);
            ptr=ptr->next;
        }
        return *this;
    }
    deque(const std::initializer_list<Object>& rhs)
    {
        init();
        for(auto x : rhs)
            inject(x);
    }
    int size()
    {
        return theSize;
    }
    bool empty()
    {
        return size()==0;
     } 
    void push(const Object& x)
    {
        head->next=head->next->prev=new Node(x,head,head->next);
        ++theSize;
    }
    const Object& pop()
    {
        if(!empty())
        {
            Node* ptr= head->next;
            Object x = ptr->data;
            head->next=ptr->next;
            ptr->next->prev=head;
            delete ptr;
            --theSize;
            return x;
           }
           else
           {
               std::cout << "error: empty deque\n";
               return Object();
           }
    }
    void inject(const Object& x)
    {
        tail->prev=tail->prev->next=new Node(x,tail->prev,tail);
        ++theSize;
    }
    const Object& eject()
    {
        if(!empty())
        {
            Node* ptr=tail->prev;
            Object x= ptr->data;
            tail->prev=ptr->prev;
            ptr->prev->next=tail;
            delete ptr;
            --theSize;
            return x;
        }
        else
           {
               std::cout << "error: empty deque\n";
               return Object();
           }
    }
    void clear()
    {
        Node* ptr=head->next;
        while(ptr!=tail)
        {
            Node* pn = ptr->next;
            delete ptr;
            ptr= pn;
            --theSize;
        }
    }
    void print()
    {
        Node* ptr=head->next;
        while(ptr!=tail)
        {
            std::cout << ptr->data << ' ';
            ptr = ptr->next;
        }
        std::cout << '\n';
    }
};

int main()
{
    deque<int> d{1,2,3,4,5,6};
    d.inject(9);
    d.inject(10);
    d.eject();
    deque<int> d2=d;
    d2.print();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lhb666aboluo/p/12822902.html