C++双向循环链表

#include<stdio.h>
#include<iostream>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

typedef int DataType;

struct Node
{
    Node* _pNext;
    Node* _pPre;
    DataType _data;

    Node(const DataType& data = DataType())
        : _pNext(NULL)
        , _pPre(NULL)
        , _data(data)
    {}
};

class List
{
public:
    List()
        : _pHead(new Node)
    {}

    List(const List& l)                            //拷贝构造
    {
        List tmp(l);
        swap(_pHead, tmp._pHead);
    }

    List& operator=(const List& l)                 //赋值运算符
    {
        if (this != &l)
        {
            List tmp(l);
            swap(_pHead, tmp._pHead);
        }
        return *this;
    }

    ~List()                                        //析构
    {
        if (NULL != _pHead)
        {
            delete _pHead;
            _pHead = NULL;
        }
    }
    void PushBack(DataType data)
    {
        if (NULL == _pHead->_pPre)                  //空
        {
            _pHead = BuyNode(data);
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        else if (_pHead->_pNext == _pHead)         //有一个
        {
            Node* NewNode = BuyNode(data);
            _pHead->_pNext = NewNode;
            _pHead->_pPre = NewNode;
            NewNode->_pNext = _pHead;
            NewNode->_pPre = _pHead;
        }
        else if (_pHead->_pNext != _pHead)                             //多个
        {
            Node* NewNode = BuyNode(data);
            Node* pcur = _pHead;
            while (pcur->_pNext != _pHead)
            {
                pcur = pcur->_pNext;
            }
            pcur->_pNext = NewNode;
            NewNode->_pPre = pcur;
            NewNode->_pNext = _pHead;
            _pHead->_pPre = NewNode;
        }
    }
    void PopBack()
    {
        assert(_pHead != NULL);
        if (NULL == _pHead->_pPre)                                      //空
        {
            return;
        }
        else if (_pHead->_pNext == _pHead)                             //一个
        {
            delete(_pHead);
            _pHead = NULL;
        }
        else if (_pHead->_pNext != _pHead)                             //多个
        {
            Node* pcur = _pHead;
            while (pcur->_pNext != _pHead)
            {
                pcur = pcur->_pNext;
            }
            Node* ppre = pcur->_pPre;
            ppre->_pNext = _pHead;
            _pHead->_pPre = ppre;
            delete pcur;
            pcur = NULL;
        }
    }
    void PushFront(DataType data)
    {
        if (NULL == _pHead->_pPre)                  //空
        {
            _pHead = BuyNode(data);
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        else if (_pHead->_pNext == _pHead)         //有一个
        {
            Node* NewNode = BuyNode(data);
            NewNode->_pNext = _pHead;
            NewNode->_pPre = _pHead;
            _pHead->_pNext = NewNode;
            _pHead->_pPre = NewNode;
            _pHead = NewNode;
        }
        else if (_pHead->_pNext != _pHead)
        {
            Node* NewNode = BuyNode(data);
            Node* ptail = _pHead;
            while (ptail->_pNext != _pHead)
            {
                ptail = ptail->_pNext;
            }
            NewNode->_pNext = _pHead;
            _pHead->_pPre = NewNode;
            ptail->_pNext = NewNode;
            NewNode->_pPre = ptail;
            _pHead = NewNode;
        }
    }

    void PopFront()
    {
        assert(_pHead != NULL);
        if (NULL == _pHead->_pPre)                                      //空
        {
            return;
        }
        else if (_pHead->_pNext == _pHead)                             //一个
        {
            delete(_pHead);
            _pHead = NULL;
        }
        else if (_pHead->_pNext != _pHead)                             //多个
        {
            Node* pcur = _pHead;
            Node* ptail = _pHead;
            while (ptail->_pNext != _pHead)
            {
                ptail = ptail->_pNext;
            }
            Node* pnext = _pHead->_pNext;
            pnext->_pPre = ptail;
            ptail->_pNext = pnext;
            _pHead = pnext;
            delete pcur;
        }
    }
    Node* Find(DataType data)
    {
        assert(_pHead != NULL);
        Node* pcur = _pHead;
        while (pcur->_pNext != _pHead)
        {
            if (pcur->_data == data)
            {
                return pcur;
            }
            pcur = pcur->_pNext;
        }
        return NULL;
    }
    void Insert(Node* pos, DataType data)
    {
        assert(pos != NULL);
        Node* pin = BuyNode(data);
        Node* pnext = pos->_pNext;
        pos->_pNext = pin;
        pin->_pNext = pnext;
        pin->_pPre = pos;
        pnext->_pPre = pin;
    }

    void Erase(Node* pos)
    {
        Node* pon = pos->_pPre;
        Node* pun = pos->_pNext;
        pon->_pNext = pun;
        pun->_pPre = pon;
        delete pos;
    }

    size_t Size()
    {
        assert(_pHead != NULL);
        size_t k = 1;
        Node* pcur = _pHead;
        while (pcur->_pNext != _pHead)
        {
            pcur = pcur->_pNext;
            k++;
        }
        return k;
    }
    bool Empty()const
    {
        return _pHead == NULL;
    }
    void Print(void)
    {
        if (_pHead == NULL)
        {
            cout << "链表已空" << endl;
            return;
        }
        Node* pcur = _pHead;
        while (pcur->_pNext != _pHead)
        {
            cout << pcur->_data << " ";
            pcur = pcur->_pNext;
        }
        cout << pcur->_data << endl;
    }
private:
    Node * BuyNode(const  DataType& data)
    {
        return new Node(data);
    }
private:
    Node * _pHead;
};

int main(void)
{
    List s1;
    s1.PushBack(1);
    //s1.PopFront();
    s1.PushBack(2);
    s1.PushBack(3);
    s1.PushBack(4);
    s1.PushBack(5);
    //s1.PopBack(); 
    Node* pos = s1.Find(3);
    s1.Insert(pos, 5);
    s1.Erase(pos);
    s1.Print();
    cout << s1.Size() << endl;
    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值