C++模板类实现链表

来源:互联网 发布:宁夏干部网络培训登录 编辑:程序博客网 时间:2024/06/15 03:48
#pragma once#include <assert.h>template<class T>struct ListNode{    T _data;    ListNode<T>* _next;    ListNode<T>* _prev;    ListNode(const T& x)        :_data(x)        ,_next(NULL)        ,_prev(NULL)    {}};template<class T>class List{    typedef ListNode<T> Node;public:    List()        :_head(NULL)        ,_tail(NULL)    {}    // l2(l1)    List(const List<T>& l)        :_head(NULL)        ,_tail(NULL)    {        Node* cur = l._head;        while(cur)        {            this->PushBack(cur->_data);            cur = cur->_next;        }    }    // l1 = l2    List<int>& operator=(List<T> l)    {        swap(l._head, _head);        swap(l._tail, _tail);        return *this;    }    void Clear()    {        Node* cur = _head;        while (cur)        {            Node* next = cur->_next;            delete cur;            cur = next;        }        _head = _tail = NULL;    }    ~List()    {        Clear();    }    void PushBack(const T& x)    {        //1.空        //2.多个节点        if(_head == NULL)        {            _head = _tail = new Node(x);        }        else        {            Node* tmp = new Node(x);            _tail->_next = tmp;            tmp->_prev = _tail;            _tail = tmp;        }    }    void PopBack()    {        // 1.空        // 2.一个节点        // 3.多个节点    /*  if(_head == NULL)        {            return;        }        else if (_head == _tail)        {            delete _head;            _head = _tail = NULL;        }        else        {            Node* prev = _tail->_prev;            delete _tail;            _tail = prev;            _tail->_next = NULL;        }*/        Erase(_tail);    }    void PushFront(const T& x)    {        Insert(_head, x);    }    void PopFront()    {        Erase(_head);    }    const T& Front()    {        assert(_head);        return _head->_data;    }    const T& Back()    {        assert(_tail);        return _tail->_data;    }    Node* Find(const T& x)    {        Node* cur = _head;        while(cur)        {            if (cur->_data == x)            {                return cur;            }            cur = cur->_next;        }        return NULL;    }    // pos的前面插入    // 设计一个双向链表,实现随机位置的插入和删除    //     void Insert(Node* pos, const T& x)    {        assert(pos);        Node* prev = pos->_prev;        Node* next = pos;        Node* cur = new Node(x);        if (prev)        {            prev->_next = cur;            cur->_prev = prev;        }        cur->_next = next;        next->_prev = cur;    }    void Erase(Node* pos)    {        assert(pos);        if (_head == NULL)        {            return;        }        else if (_head == _tail)        {            assert(pos == _head);            delete _head;            _head = _tail = NULL;        }        else if (pos->_prev == NULL)        {            Node* next = pos->_next;            delete pos;            _head = next;            _head->_prev = NULL;        }        else if (pos->_next == NULL)        {            Node* prev = pos->_prev;            delete pos;            _tail = prev;            _tail->_next = NULL;        }        else        {            Node* prev = pos->_prev;            Node* next = pos->_next;            delete pos;            prev->_next = next;            next->_prev = prev;        }    }    void Print()    {        Node* cur = _head;        while (cur)        {            cout<<cur->_data<<" ";            cur = cur->_next;        }        cout<<endl;    }    bool Empty()    {        return _head == NULL;    }protected:    Node* _head;    Node* _tail;};

测试代码如下:

void TestList1(){    List<int> l1;    l1.PushBack(1);    l1.PushBack(2);    l1.PushBack(3);    l1.PushBack(4);    l1.Print();    l1.PopBack();    l1.PopBack();    l1.PopBack();    l1.Print();}// void TestList2(){    List<int> l1;    l1.PushBack(1);    l1.PushBack(2);    l1.PushBack(3);    l1.PushBack(4);    l1.Print();    ListNode<int>* pos = l1.Find(3);    l1.Insert(pos, 30);    l1.Print();    pos = l1.Find(4);    l1.Insert(pos, 40);    l1.Erase(pos);    l1.Print();}// void TestList3(){    List<int> l1;    l1.PushBack(1);    l1.PushBack(2);    l1.PushBack(3);    l1.PushBack(4);    l1.Print();    List<int> l2(l1);    l2.Print();    List<int> l3;    l3.PushBack(4);    l3.PushBack(5);    l1 = l3;    l1.Print();}