模拟实现C++中的ListIterator函数

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
template<class T>
struct ListNode
{
    ListNode(const T& data = T())
    : _pNext(NULL)
    , _pPre(NULL)
    , _data(data)
    {}

    ListNode* _pNext;
    ListNode* _pPre;
    T _data;
};

template<class T>
class ListIterator
{
    typedef ListNode* PNode;
    typedef ListIterator Self;
public:
    ListIterator()
        : _pCur(NULL)
    {}

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

    ListIterator(const Self& s)
        : _pCur(s._pCur)
    {}

    Self& operator++()
    {
        _pCur = _pCur->_pNext;
        return *this;
    }

    Self operator++(int)
    {
        Self temp(*this);
        _pCur = _pCur->_pNext;
        return temp;
    }

    Self& operator--()
    {
        _pCur = _pCur->_pPre;
        return *this;
    }

    Self operator--(int)
    {
        Self temp(*this);
        _pCur = _pCur->_pPre;
        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 Node;
    typedef Node* PNode;

public:
    typedef ListIterator Iterator;
public:
    List()
    {
        CreateListHead();
    }

    List(T* array, size_t size)
    {
        CreateListHead();//先有一个表头结点
        for (int i = 0; i < size; i++)//不用memcpy函数:会产生浅拷贝
        {
            PushBack(array[i]);//尾插元素
        }
    }
    List(const List& l)
    {
        _phead = new Node;
        PNode ptail = l->pNext;
        while (ptail == l)
        {
            PushBack(ptail->data);
            ptail = ptail->pNext;
        }
    }
    List& operator=(const List& l)
    {
        if (this == &l)
        {
            return *this;
        }
        _phead = new Node;
        PNode ptail = l->pNext;
        while (ptail == l)
        {
            PushBack(ptail->data);
            ptail = ptail->pNext;
        }
    }
    ~List()
    {
        Clear();//清空链表中的元素,表头结点还在
        delete _pHead;
        _pHead = NULL;
    }


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

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

    void PushBack(const T& data)
    {
        PNode ptail = _pHead->_pPre;

        PNode pNewNode = new Node(data);
        ptail->_pNext = pNewNode;
        pNewNode->_pNext = _pHead;
        pNewNode->_pPre = ptail;
        _pHead->_pPre = pNewNode;
    }
    void PopBack()
    {
        PNode ptail = _pHead->_pPre;

        ptail->_pPre->_pNext = _pHead;
        _pHead->_pPre = ptail->_pPre;
        delete ptail;
    }
    void PushFront(const T& data)
    {
        PNode ptail = new Node(data);

        ptail->_pNext = _pHead->_pNext;
        -pHead->_pNext->_pPre = ptail;
        ptail->_pPre = _pHead;
        _pHead->_pNext = ptail;
    }
    void PopFront()
    {
        if (_pHead->_pNext == _phead)
            return;
        else
        {
            PNode ptail = _pHead->_pNext;
            _pHead->_pNext = ptail->_pNext;
            ptail->_pNext->pPre = _pHead;
            delete ptail;
        }
    }
    PNode Insert(PNode pos, const T& data)
    {
        PNode ptail = pos->_pPre;
        PNode pNewNode = new Node(data);
        pNewNode->_pNext = ptail;
        ptail->_pPre = pNewNode;
        pos->_pNext = pNewNode;
        pNewNode->_pPre = pos;
    }
    PNode Erase(PNode pos)
    {
        pos->_pPre->_pNext = pos->_pNext;
        pos->_pNest->pPre = pos->_pPre;
        delete pos;
    }

    size_t Size()
    {
        int count = 0;
        PNode ptail = _pHead->_pNext;
        while (ptail != _pHead)
        {
            count++;
            ptail = ptail->_pNext;
        }
        return count;
    }
        // 将链表中的结点清空 
    void Clear()
    {
        PNode ptail = _pHead->_pNext;
        while (ptail != _pHead)
        {
            _pHead->_pNext = ptail->_pNext;
            delete ptail;
            ptail = _pHead->_pNext;
        }
        ptail->_pPre = _pHead;
    }
    bool Empty()const
    {
        return _pHead->_pNext - _pHead;
    }
private:
    void CreateListHead()
    {
        _pHead = new Node;
        _pHead->_pNext = _pHead;
        _pHead->_pPre = _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 start;
}

int main(void)
{
    size_t i;
    List<int> L1;
    L1.PushBack(1);
    L1.PushBack(2);
    L1.PushBack(3);
    L1.PushBack(4);
    L1.PopBack();
    List<int>::Iterator it = L1.Begin();
    while (L1.End() != it)
    {
        cout << *it << "  ";
        ++it;
    }
    cout << endl;
    i = L1.Size();
    cout << L1.Empty() << endl;
    cout << i << endl;
    cout << L1.Empty() << endl;
    i = L1.Size();
    cout << i << endl;
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值