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();

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您讲解一下如何模拟STL的list类。 List是一个双向链表,我们可以通过自己实现节点类和链表类来模拟这个数据结构。 首先,我们需要定义一个节点类,包含前驱节点指针、后继节点指针、以及存储数据的成员变量。代码如下: ``` template <typename T> class ListNode { public: T data; ListNode* prev; ListNode* next; ListNode(T data) : data(data), prev(nullptr), next(nullptr) {} }; ``` 接着,我们定义链表类,包含头节点指针和尾节点指针,以及一些常用的操作方法,如插入、删除、查找等。代码如下: ``` template <typename T> class List { public: List() : head(nullptr), tail(nullptr), size(0) {} ~List() { clear(); } void insert(T data) { ListNode<T>* node = new ListNode<T>(data); if (head == nullptr) { head = node; tail = node; } else { tail->next = node; node->prev = tail; tail = node; } size++; } void remove(T data) { ListNode<T>* node = head; while (node != nullptr) { if (node->data == data) { if (node == head) { head = node->next; if (head != nullptr) { head->prev = nullptr; } } else if (node == tail) { tail = node->prev; if (tail != nullptr) { tail->next = nullptr; } } else { node->prev->next = node->next; node->next->prev = node->prev; } delete node; size--; return; } node = node->next; } } ListNode<T>* find(T data) { ListNode<T>* node = head; while (node != nullptr) { if (node->data == data) { return node; } node = node->next; } return nullptr; } void clear() { ListNode<T>* node = head; while (node != nullptr) { ListNode<T>* next = node->next; delete node; node = next; } head = nullptr; tail = nullptr; size = 0; } int getSize() const { return size; } private: ListNode<T>* head; ListNode<T>* tail; int size; }; ``` 这样,我们就实现了一个简单的模拟STL的list类。您可以通过调用insert、remove、find等方法来操作链表中的元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值