C++实现双向链表

来源:互联网 发布:mysql linux 编辑:程序博客网 时间:2024/05/21 06:39

双向链表也叫双链表,它的每个数据节点有两个指针,一个指向前节点,一个指向后节点。在实现插入操作时就要注意指针的指向。以下是双链表的基本实现:

//C++实现双向链表#include<iostream>#include<assert.h>using namespace std;typedef int DataType;struct ListNode{    ListNode* _prev;    ListNode* _next;    DataType _data;    ListNode(DataType x)        :_prev(NULL)        ,_next(NULL)        ,_data(x)    {}};class List{    typedef ListNode Node;public:    List()        :_head(NULL)        ,_tail(NULL)    {}    List(const List& l)        :_head(NULL)        ,_tail(NULL)    {        Copy(l);    }    List& operator=(const List& l)//先销毁,在拷贝(用PushBack深拷贝)    {        if(this != &l)        {            Destroy();            Copy(l);        }        return *this;    }    ~List()    {        Node* cur = _head;        while(cur)        {            Node* del = cur;            cur = cur->_next;            delete del;        }        _head = _tail = NULL;    }    void PushBack(DataType x)    {        if(_head == NULL)            _head = _tail = new Node(x);        else        {            Node* tmp = new Node(x);            _tail->_next = tmp;            tmp->_prev = _tail;            _tail = tmp;        }    }    void PopBack()    {        //Erase(_tail);        if(_head == NULL)            return;        else if(_head->_next == NULL)        {            delete _head;            _head = _tail = NULL;        }        else        {            Node* prev = _head;            while(prev->_next->_next)            {                prev = prev->_next;            }            delete _tail;            _tail = prev;            _tail->_next = NULL;        }    }    void PushFront(DataType x)    {        if(_head == NULL)            _head = _tail = new Node(x);        else            Insert(_head,x);//复用代码    }    void PopFront()    {        //Erase(_head);        if(_head == NULL)            return;        else if(_head->_next == NULL)        {               delete _head;            _head = _tail = NULL;        }        else        {            Node* next = _head;            next = _head->_next;            delete _head;            _head = next;            _head->_prev = NULL;        }    }    //在pos的前面插入    void Insert(Node* pos,DataType x)    {        assert(pos);        if(pos == _head)        {            Node* tmp = new Node(x);            tmp->_next = _head;            _head->_prev = tmp;            _head = tmp;            _head->_prev = NULL;        }        else        {            Node* cur = pos;            Node* prev = cur->_prev;            Node* tmp = new Node(x);            prev->_next = tmp;            tmp->_next = cur;            cur->_prev = tmp;            tmp->_prev = prev;        }    }    void Erase(Node* pos)    {        assert(pos);        Node* prev = pos->_prev;        Node* next = pos->_next;        if(prev==NULL && next==NULL)//只有一个节点        {            assert(pos == _head);            _head = _tail = NULL;        }        else if(prev == NULL)//头删        {            _head = next;            _head->_prev = NULL;        }        else if(next == NULL)//尾删        {            _tail = prev;            _tail->_next = NULL;        }        else //中间删        {            prev->_next = next;            next->_prev = prev;        }        delete pos;    }    Node* Find(DataType x)    {        Node* cur = _head;        while(cur)        {            if(cur->_data == x)                return cur;            cur = cur->_next;        }        return NULL;    }    //void Reverse()//方法一:改变指针的指向    //{    //  Node* cur = _head;    //  while(cur)    //  {    //      swap(cur->_prev,cur->_next);//std::swap    //      cur = cur->_prev;    //  }    //  swap(_head,_tail);    //}    void Reverse()//方法二:交换首尾的数据    {        Node* left = _head;        Node* right = _tail;        while(!(left == right || left->_prev == right))        {            swap(left->_data,right->_data);            left = left->_next;            right = right->_prev;        }    }    void Destroy()    {        if(_head == NULL)            return;        else        {            Node* cur = _head;            while(cur)            {               Node* del = cur;               cur = cur->_next;               delete del;            }        }        _head = _tail = NULL;    }    void Copy(const List& l)    {        Node* cur = l._head;        while(cur)        {            this->PushBack(cur->_data);            cur = cur->_next;        }    }    void Print()    {        Node* cur = _head;        while(cur)        {            cout<<cur->_data<<" ";            cur = cur->_next;        }        cout<<endl;    }private:    Node* _head;    Node* _tail;};

测试用例:

void TestList1(){    List l1;    l1.PushBack(1);    l1.PushBack(2);    l1.PushBack(3);    l1.PushBack(4);    l1.Print();    List l2(l1);    List l3;    l3 = l1;    l2.Print();    l3.Print();    //l1.Reverse();    //l1.Print();    //l1.PopBack();    //l1.PopBack();    //l1.PopBack();    //l1.PopBack();    //l1.PopBack();    //l1.Print();    //ListNode* pos = l1.Find(3);    //l1.Insert(pos,33);    //l1.Print();    //l1.Erase(pos);    //l1.Print();    //ListNode* ret = l1.Find(1);    //l1.Insert(ret,55);    //l1.Print();    //l1.Erase(ret);    //l1.Print();}void TestList2(){    List l;    l.PushFront(1);    l.PushFront(2);    l.PushFront(3);    l.PushFront(4);    l.Print();    ListNode* ret = l.Find(2);    cout<<ret->_data;    l.PopFront();    l.PopFront();    l.PopFront();    l.PopFront();    l.PopFront();    l.Print();}

主函数:

#define _CRT_SECURE_NO_WARNINGS 1//#include"Seqlist.h"//#include"SList.h"#include"List.h"int main(){    //TestSeqList1();    //TestSeqList2();    //TestSList1();    //TestSList2();    //TestSList3();    //TestList1();    TestList2();    return 0;}
原创粉丝点击