C++实现双向链表

来源:互联网 发布:无网络摄像头安装方法 编辑:程序博客网 时间:2024/06/13 23:53

C++实现双链表代码如下:

#pragma once#include<iostream>#include"assert.h"#include"string.h"using namespace std;typedef int DataType;struct ListNode{    ListNode* _next;    ListNode* _prev;    DataType _data;    ListNode(DataType x)         :_next(NULL)        , _prev(NULL)        , _data(x)    {    }};class List{    typedef ListNode Node;public:    List():_head(NULL), _tail(NULL)    {   }    List(const List& l) :_head(NULL), _tail(NULL)    {        Node* cur = l._head;        while (cur)        {            Node* tmp =new Node(cur->_data);            if (_tail == NULL)            {                _head = _tail = tmp;            }            else            {                _tail->_next = tmp;                _tail = tmp;            }            cur = cur->_next;        }    }    List& operator=(List l)    {        swap(_head, l._head);        swap(_tail, l._tail);        return *this;    }    ~List()    {        Node* cur = _head;        while (cur)        {            Node* next = cur->_next;            delete cur;            cur = next;        }        _head = _tail = NULL;    }    void PushBack(DataType x)    {        Node* tmp = new Node(x);        if (_tail == NULL)        {            _head = _tail = tmp;        }        else        {                       tmp->_prev = _tail;            _tail->_next = tmp;            _tail = tmp;        }    }    void PopBack()    {        if (_tail == NULL)        {            cout << "It is empty!" << endl;        }        else        {            Node* cur = _tail->_prev;            delete _tail;            cur->_next = NULL;                      _tail = cur;                    }    }    void PushFront(DataType x)    {        Node* tmp = new Node(x);        if (_tail == NULL)        {            _head = _tail = tmp;        }        else        {            _head->_prev = tmp;            tmp->_next = _head;            _head = tmp;        }    }    void PopFront()    {        if (_tail == NULL)        {            cout << "It is empty!" << endl;        }        else        {            Node* next = _head->_next;            delete _head;            next->_prev = NULL;            _head = next;        }    }    Node* Find(DataType x)    {        Node* cur = _head;        while (cur)        {            if (cur->_data == x)            {                return cur;            }            cur = cur->_next;        }        return NULL;    }    // 在pos的前面插入一个     void Insert(Node* pos, DataType x)    {        if (pos == _head)        {            PushFront(x);        }        else        {            Node* tmp = new Node(x);            Node* cur = pos->_prev;            cur->_next = tmp;            tmp->_next = pos;            pos->_prev = tmp;            tmp->_prev = cur;                   }    }    void Erase(Node* pos)    {        if (pos == _head)        {            PopFront();        }        else if (pos == _tail)        {            PopBack();        }        else        {            Node* next = pos->_next;            Node* prev = pos->_prev;            delete pos;            prev->_next = next;            next->_prev = prev;        }    }    void Reverse()//逆序    {        Node* begin = _head;        Node* end = _tail;        while ((begin != end)&&(begin->_prev!=end))//针对奇数和偶数个Node        {            swap(begin->_data, end->_data);            begin = begin->_next;            end = end->_prev;        }    }    //void Reverse()//逆序    //{    //  Node* cur = _head;    //  while (cur)    //  {    //      swap(cur->_prev, cur->_next);    //      cur = cur->_prev;    //  }    //  swap(_head, _tail);    //}    void Print()    {        Node* cur = _head;        while (cur)        {            cout << cur->_data << "  ";            cur = cur->_next;        }        cout << endl;    }private:    Node* _head;    Node* _tail;};

测试代码如下:

#include"List.h"void Testlist(){    List l1;    l1.PushBack(1);    l1.PushBack(2);    l1.PushBack(3);    l1.PushBack(4);    l1.Print();    l1.PushFront(4);    l1.PushFront(3);    l1.PushFront(2);    l1.PushFront(1);    l1.Print();    List l2(l1);    l2.Print();    List l3 = l2;    l3.Print();    l1.PopFront();    l1.PopFront();    l1.PopBack();    l1.PopBack();    l1.Print();    l1.Reverse();    l1.Print();    l1.Insert(l1.Find(3), 100);    l1.Insert(l1.Find(2), 100);    l1.Print();    l1.Erase(l1.Find(1));    l1.Print();}int main(){    Testlist();    system("pause");    return 0;}

测试运行结果如下:
这里写图片描述