C++实现双链表

来源:互联网 发布:离线写作软件 编辑:程序博客网 时间:2024/06/10 22:16

什么是双链表呢

双链表

双链表它的每个数据节点中都有两个指针,分别指向直接后继和直接前驱。从双链表的任意一个节点开始,都可以很方便的访问它的前驱节点和后继节点。学习了C++后,我试着用C++实现了一份简单的双链表。

头文件

# pragma once#include<iostream>#include<assert.h>using namespace std;typedef int datatype;struct Node{    Node(const datatype data = 0)        :_data(data)        ,_pre(NULL)        ,_next(NULL)    {}    datatype _data;    Node* _pre;    Node* _next;};class ListNode{public:    ListNode()        :_phead(NULL)    {}    ListNode(size_t n,datatype data)//构造n个相同的非空链表    {        {            _phead = BuyNode(data);            Node* cur = _phead;            while(n--)            {                cur->_next = BuyNode(data);                cur->_next->_pre = cur;                cur = cur->_next;            }        }    }    void PushBack(const datatype& data)    {        Node* tmp = _phead;        Node* NewNode = BuyNode(data);        if(_phead == NULL)//若原先链表为空,则头指针指向新节点        {            _phead = BuyNode(data);        }        else        {            while(tmp->_next != NULL)            {                tmp = tmp->_next;            }            tmp->_next = NewNode;            NewNode->_pre = tmp;            NewNode->_next = NULL;        }    }    void PopBack()    {        assert(_phead);        Node* Tail = FindTail();        Tail->_pre->_next = NULL;        delete Tail;        Tail = NULL;    }    void PushFront(const datatype& data)    {        Node* tmp = _phead;        Node* NewNode = BuyNode(data);        if(_phead == NULL)        {            _phead = NewNode;        }        else        {            NewNode->_next = tmp;            tmp->_pre = NewNode;            NewNode->_pre = NULL;            _phead = NewNode;        }    }    void PopFront()    {        assert(_phead != NULL);        Node* tmp = _phead;        _phead = tmp->_next;        delete tmp;        tmp = NULL;    }    void InsertFront(datatype pos,const datatype& data)    {        if(pos == _phead->_data)        {            PushFront(data);        }        else        {            Node* NewNode = BuyNode(data);            Node* Pos = Find(pos);            NewNode->_pre = Pos->_pre;            Pos->_pre->_next = NewNode;            NewNode->_next = Pos;            Pos->_pre = NewNode;        }    }    void InsertBack(datatype pos,const datatype& data)    {        Node* Tail = FindTail();        if(pos == Tail->_data)        {            PushBack(data);        }        else        {            Node* Pos = Find(pos);            Node* NewNode = BuyNode(data);            NewNode->_next = Pos->_next;            Pos->_next->_pre = NewNode;            NewNode->_pre = Pos;            Pos->_next = NewNode;        }    }    void Insert(datatype pos,const datatype& data)    {        assert(_phead != NULL);        Node* Pos = Find(pos);        Node* NewNode = BuyNode(data);        NewNode->_pre = Pos->_pre;        Pos->_pre->_next = NewNode;        NewNode->_next = Pos->_next;        Pos->_next->_pre = NewNode;        delete Pos;        Pos = NULL;    }    void Erase(datatype pos)    {        assert(_phead != NULL);        Node* Pos = Find(pos);        Pos->_pre->_next = Pos->_next;        Pos->_next->_pre = Pos->_pre;        delete Pos;        Pos = NULL;    }    void Print()    {        cout<<"NULL"<<"-";        Node* cur = _phead;        while(cur != NULL)        {            cout<<cur->_data<<"-";            cur = cur->_next;        }        cout<<"NULL"<<endl;    }private:    Node* BuyNode(const datatype& data)    {        return new Node(data);    }    Node* Find(const datatype& pos)    {        Node* cur = _phead;        while(cur->_data != pos)        {            cur = cur->_next;        }        return cur;    }    Node* FindTail()    {        assert(_phead != NULL);        Node* tmp = _phead;        while(tmp->_next != NULL)        {            tmp = tmp->_next;        }        return tmp;    }private:    Node* _phead;};

测试部分

#include"head.h"using namespace std;void Test(){    ListNode s1(2,3);    s1.Print();    s1.PushBack(4);    s1.Print();    s1.PushFront(1);    s1.Print();    s1.Insert(3,10);    s1.Print();    s1.InsertFront(10,100);    s1.Print();    s1.InsertBack(10,200);    s1.Print();    s1.Erase(3);    s1.Print();    s1.PopBack();    s1.Print();    s1.PopFront();    s1.Print();}int main(){    Test();    return 0;}

测试结果

测试结果

原创粉丝点击