list簡單模擬實現

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <assert.h>
#include <iostream>
using namespace std;
template<class T>
struct ListNode
{
    ListNode(const T& data = T())
    :_pPre(0)
    , _pNext(0)
    , _data(data)
    {}
    ListNode<T>* _pPre;
    ListNode<T>* _pNext;
    T _data;
};


template<class T, class Ref, class Ptr>
class ListIterator         // 簡單迭代器的實現
{
public:
    typedef ListIterator Self;
    ListIterator()
        :_pCur(0)
    {
    }
    ListIterator(const ListIterator& I)
        :_pCur(I._pCur)
    {}
    ListIterator( ListNode<T>* ptr)
        :_pCur(ptr)
    {
    }
    Ref operator*()
    {
        return _pCur->_data;
    }
    Ptr operator->()
    {
        return &(_pCur->_data);
    }
    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 temp;
    }
    bool operator!=(const Self& s)
    {
        return s._pCur != _pCur;
    }
    bool operator==(const Self& s)
    {
        return s._pCur == _pCur;
    }

public:
    ListNode<T>* _pCur;
};

template<class T>
class List
{
public:
    typedef ListIterator<T, T&,T*>  Iterator;   
    List()
        :_pHead(new ListNode<T>)
    {
        _pHead->_pNext = _pHead;
        _pHead->_pPre = _pHead;
    }
    List(const T* array, size_t size)
        :_pHead(new ListNode<T>)
    {
        _pHead->_pNext = _pHead;
        _pHead->_pPre = _pHead;
        for (size_t i = 0; i < size; ++i)
            PushBack(array[i]);

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

    }

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

    Iterator End()
    {
        return Iterator(_pHead);
    }
    void PushBack(const T& data)
    {
        ListNode<T>* pNewNode = new  ListNode<T>(data);
        if (Empty())
        {
            pNewNode->_pNext = _pHead;
            _pHead->_pNext = pNewNode;
            _pHead->_pPre = pNewNode;
            pNewNode->_pPre = _pHead;
        }
        else
        {
            ListNode<T>* pTail = _pHead->_pPre;
            pTail->_pNext = pNewNode;
            pNewNode->_pPre = pTail;
            pNewNode->_pNext = _pHead;
            _pHead->_pPre = pNewNode;
        }
    }
    void PopBack()
    {
        if (Empty())
            return;
        ListNode<T>* pTail = _pHead->_pPre->_pPre;
        delete pTail->_pNext;
        pTail->_pNext = _pHead;
        _pHead->_pPre = pTail;
    }
    void PushFront(const T& data)
    {
        ListNode<T>* pNewNode = new  ListNode<T>(data);
        pNewNode->_pNext = _pHead->_pNext;
        pNewNode->_pPre = _pHead;
        _pHead->_pNext->_pPre = pNewNode;
        _pHead->_pNext = pNewNode;
    }
    void PopFront()
    {
        if (Empty())
            return;
        ListNode<T>* pTail = _pHead->_pNext;
        _pHead->_pNext = pTail->_pNext;
        pTail->_pNext->_pPre = _pHead;
        delete pTail;
    }

    Iterator Insert(Iterator pos, const T& data)
    {
        assert(pos != NULL);
        ListNode<T>*  pNewNode = new  ListNode<T>(data);

        pNewNode->_pNext = pos._pCur;
        pNewNode->_pPre = pos._pCur->_pPre;
        pos._pCur->_pPre->_pNext = pNewNode;
        pos._pCur->_pPre = pNewNode;
        return Iterator(pNewNode);

    }
    Iterator Erase(Iterator pos)
    {
        assert(pos != NULL);
        ListNode<T>* temp = pos._pCur->_pNext;
        pos._pCur->_pPre->_pNext = pos._pCur->_pNext;
        pos._pCur->_pNext->_pPre = pos._pCur->_pPre;
        delete (pos._pCur);
        return Iterator(temp);

    }
    bool Empty()const
    {
        return _pHead->_pNext == _pHead->_pPre;

    }
    size_t Size()const
    {
        size_t size = 0;
        ListNode<T>* _cur = _pHead->_pNext;
        while (_cur != _pHead)
        {
            size++;
            _cur = _cur->_pNext;
        }
        return size;
    }
    T& Front()
    {
        assert(Empty() != true );
        return _pHead->_pNext->_data;

    }
    const T& Front()const
    {
        assert(Empty() != true);
        return _pHead->_pNext->_data;
    }
    T& Back()
    {
        assert(Empty() != true);
        return _pHead->_pPre->_data;
    }
    const T& Back()const
    {
        assert(Empty() != true);
        return _pHead->_pPre->_data;

    }
    void Clear()
    {
        Iterator temp = Begin();
        ListNode<T>* ptr = temp._pCur;
        while (temp != End())
        {
            ptr = temp._pCur;
            temp++;
            delete (ptr);
        }
        _pHead->_pNext = _pHead;
        _pHead->_pPre = _pHead;
    }
    bool Empty()
    {
        return _pHead == _pHead->_pNext;
    }

private:
    ListNode<T>* _pHead;
};
template <class T1, class T2, class T3>
T1 Find(T1 begin, T2 end, const T3 data)
{
    while (begin != end)
    {
        if (*begin == data)
            return begin;
        begin++;
    }
    return NULL;

}


void TestList1()
{
    List<int> L;

    L.PushBack(1);
    L.PushBack(2);
    L.PushBack(3);
    L.PushBack(4);
    L.PushBack(5);
    L.PushBack(6);
    List<int>::Iterator It = L.Begin();
    while (It != L.End())
    {
        cout << *It << "->";
            It++;
    }
    cout << "NULL" << endl;
    L.PopBack();
    L.PopBack();
    L.PopBack();
    It = L.Begin();
    while (It != L.End())
    {
        cout << *It << "->";
        It++;
    }
    cout << "NULL" << endl;
    cout << L.Back() << endl;
    cout << L.Front() << endl;
    cout << L.Size() << endl;
    L.Clear();

}
void TestList2()
{

    List<int> L;

    L.PushFront(1);
    L.PushFront(2);
    L.PushFront(3);
    L.PushFront(4);
    L.PushFront(5);
    L.PushFront(6);
    List<int>::Iterator It = L.Begin();
    while (It != L.End())
    {
        cout << *It << "->";
        It++;
    }
    cout << "NULL" << endl;
    L.PopFront();
    L.PopFront();
    L.PopFront();
    if (L.Empty() == true)
    {
        cout << "Empty" << endl;
    }
    It = L.Begin();
    while (It != L.End())
    {
        cout << *It << "->";
        It++;
    }
    cout << "NULL" << endl;

}
void TestList3()
{
    int arr[] = {1, 2, 3, 4, 5};
    List<int> L(arr, sizeof(arr)/sizeof(arr[0]));
    List<int>::Iterator pos(NULL);

    L.PushBack(6);
    List<int>::Iterator It = L.Begin();
    while (It != L.End())
    {
        cout << *It << "->";
        It++;
    }
    cout << "NULL" << endl;
     pos = Find(L.Begin(), L.End(), 2);
    //L.Insert(pos, 9);
    //It = L.Begin();
    //while (It != L.End())
    //{
    //  cout << *It << "->";
    //  It++;
    //}
    //cout << "NULL" << endl;

    pos = Find(L.Begin(), L.End(), 5);
    L.Erase(pos);
    It = L.Begin();
    while (It != L.End())
    {
        cout << *It << "->";
        It++;
    }
    cout << "NULL" << endl;
    cout << L.Back() << endl;
    cout << L.Front() << endl;
    cout << L.Size() << endl;
    L.Clear();

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章