链队模板


#include 

using namespace std;

template
struct Node{
    Type data;
    Node *next;
    Node(Node *ptr = nullptr):next(ptr){}
    Node(const Type &val,Node *ptr = nullptr):data(val),next(ptr){}
};


template
class ListQueue{
    Node *head,*tail;
    int len;
public:
    ListQueue(){
        head = new Node;
        tail = head;
        len = 0;
    }
    ListQueue(const ListQueue &src){
        head = new Node;
        tail = head;
        len = 0;///要先初始化本身,不然会赋值失败,clear的问题
        *this = src;
    }
    ~ListQueue(){
        clear();
        delete head;
        tail = head = nullptr;
    }


    ListQueue & operator=(const ListQueue &src){
        clear();
        Node *p = src.head->next;
        while(p){
            (*this).push(p->data);
            p = p->next;
        }
        return *this;
    }


    void push(const Type &val){
        tail = tail->next = new Node(val);
        len++;
    }


    void clear(){
        Node *p = head->next;
        while(p){
            Node *ptmp = p;
            p = p->next;
            delete ptmp;
        }
        head->next = nullptr;
        tail = head;
        len = 0;
    }
    void pop(){
        assert(len>0);
        Node *p = head;
        Node *ptmp = p->next;
        p->next = p->next->next;
        delete ptmp;
        if(len == 1) tail = p;
        len--;
    }


    int size()const{return len;}
    bool empty()const{return !len;}

    Type & front(){
        assert(len>0);
        return head->next->data;
    }
    const Type & front()const{
        assert(len>0);
        return head->next->data;
    }
    Type & back(){
        assert(len>0);
        return tail->data;
    }
    const Type & back()const{
        assert(len>0);
        return tail->data;
    }

    void create(const vector &src){
        clear();
        for(int i = 0;i *p = head->next;
        cout<<"front-->";
        while(p){
            cout<data<<(p->next?"->":"");
            p = p->next;
        }
        if(head->next) cout<<"-->";
        cout<<"back";
        cout< v;
    for(int i = 0;i<10;i++) v.push_back(i);

    ListQueue lq;
    lq.create(v);
    lq.write();

    lq.pop();
    lq.write();

    cout< lqc = lq;
    cout<