欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

模板实现带头结点的双向循环链表

程序员文章站 2024-03-21 13:36:04
...
#pragma once
#include<iostream>
using namespace std;


template<class T>
struct ListNode{                 //双向链表
    ListNode(const T& data = T())
    :_next(NULL)
    , _prev(NULL)
    , _data(data)
    {}
    ListNode<T>* _next;          //指向下一个结点的指针域
    ListNode<T>* _prev;          //指向前一个结点的指针域
    T _data;                     //值域
};

//迭代器:对原生态的指针T*进行封装
template<class T>
class ListIterator
{
    typedef ListNode<T>* pNode;
    typedef ListIterator<T> Self;
public:
    ListIterator()
        :_pCur(NULL)
    {}

    ListIterator(pNode pCur)
        :_pCur(pCur)
    {}

    ListIterator(const Self& s)//拷贝构造函数
        :_pCur(s._pCur)
    {}

    Self& operator++()//前置++
    {
        _pCur = _pCur->_next;
        return *this;
    }

    Self operator++(int)//后置++
    {
        Self temp(*this);
        _pCur = _pCur->_next;
        return temp;
    }

    Self& operator--()//前置--
    {
        _pCur = _pCur->_prev;
        return *this;
    }

    Self operator--(int)//后置--
    {
        Self temp(*this);
        _pCur = _pCur->_prev;
        return *this;
    }

    bool operator!=(const Self& s)
    {
        return _pCur != s._pCur;
    }

    bool operator==(const Self& s)
    {
        return _pCur == s._pCur;
    }

    T& operator*()
    {
        return _pCur->_data;
    }

    T* operator->()
    {
        return &(_pCur->_data);
    }

private:
    pNode _pCur;
};

template<class T>
class List
{
    typedef ListNode<T> Node;
    typedef Node* pNode;
public:
    typedef ListIterator<T> Iterator;
public:
    List()
    {
        CreateListHead();
    }

    List(T* array, size_t size)
    {
        //创建链表的头结点
        CreateListHead();
        for (size_t i = 0; i < size; ++i)
            PushBack(array[i]);               //循环尾插
    }

    /*List(const List<T>& l)
    {

    }

    List<T>& operator=(const List<T>& l)
    {

    }*/ 

    ~List()
    {
        Clear();
        delete _pHead;
        _pHead = NULL;
    }

    void PushBack(const T& data)
    {
        pNode pTail = _pHead->_prev;
        pNode pNewNode = new Node(data);
        pTail->_next = pNewNode;
        pNewNode->_prev = pTail;
        pNewNode->_next = _pHead;
        _pHead->_prev = pNewNode;
    }

    void PopBack()
    {
        if (_pHead->_next == _pHead)
            return;
        pNode pTail = _pHead->_prev;
        _pHead->_prev = pTail->_prev;
        pTail->_prev->_next = _pHead;
        delete pTail;
    }

    void PushFront(const T& data)
    {
        pNode pNewNode = new Node(data);
        pNewNode->_next = _pHead->_next;
        pNewNode->_prev = _pHead;
        _pHead->_next->_prev = pNewNode;
        _pHead->_next = pNewNode;
    }
    void PopFront()
    {
        if (_pHead->_next == _pHead)
            return;
        pNode pHeadTail = _pHead->_next;
        _pHead->_next = pHeadTail->_next;
        pHeadTail->_next->_prev = _pHead;
        delete pHeadTail;
    }

    pNode Insert(pNode pos, const T& data)
    {
        assert(pos);
        pNode pNewNode = new Node(data);
            pos->_prev->_next = pNewNode;
            pNewNode->_prev = pos->_prev;
            pNewNode->_next = pos;
            pos->_prev = pNewNode;  
            return pNewNode;
    }
    pNode Erase(pNode pos)
    {

        pos->_prev->_next = pos->_next;
        pos->_next->_prev = pos->_prev;
        delete pos;
        return 
    }

    void Clear()
    {
        pNode pDel = _pHead->_next;
        while (!Empty())
        {
            pDel = _pHead->_next;
            _pHead->_next = pDel->_next;
            delete pDel;
        }
        _pHead->_prev = _pHead;
    }

    bool Empty()
    {
        return _pHead->_next == _pHead;
    }

    Iterator Begin()
    {
        return  Iterator(_pHead->_next);
    }

    Iterator End()
    {
        return  Iterator(_pHead);
    }

private:
    void CreateListHead()
    {
        _pHead = new Node;
        _pHead->_next = _pHead;
        _pHead->_prev = _pHead;
    }
private:
    pNode _pHead;
};


template<class Iterator>
Iterator Find(Iterator start, Iterator finish, const T& data)
{
    while (start != finish)
    {
        if (*start == data)
        {
            return start;
        }
        ++start;
    }
    return finish;
}

void TestList()
{
    List<int> L;
    L.PushBack(1);
    L.PushBack(2);
    L.PushBack(3);
    L.PushBack(4);
    L.PushFront(0);
    List<int>::Iterator it = L.Begin();
    while (it != L.End())
    {
        cout << *it <<" ";
        ++it;
    }
    cout << endl;
}