무민은귀여워

list 구현 본문

IT/기타

list 구현

moomini 2021. 5. 17. 23:35
반응형
#include <iostream>
#include <list>
using namespace std;
template<typename T>
class Node
{
public:
    Node()
        : pNext(nullptr)
        , pPrev(nullptr)
    {
 
    }
 
    Node(const T& InData)
        : pNext(nullptr)
        , pPrev(nullptr)
        , data(InData)
    {
 
    }
 
    Node(T&& InData)
        : pNext(nullptr)
        , pPrev(nullptr)
        , data(InData)
    {
 
    }
 
    T data;
    Node<T>* pNext;
    Node<T>* pPrev;
};
 
template<typename T>
class Mylistiterator
{
public:
    Mylistiterator(Node<T>* pThis)
    {
        pMy = pThis;
    }
 
    Mylistiterator operator++()
    {
        pMy = pMy->pNext;
 
        return *this;
    }
 
    Mylistiterator operator--()
    {
        pMy = pMy->pPrev;
 
        return this;
    }
 
    T& operator*()
    {
        return pMy->data;
    }
 
    Node<T>* operator->()
    {
        return pMy;
    }
 
    bool operator!=(const Mylistiterator<T>& InItr)
    {
        return pMy != InItr.pMy;
    }
 
    bool operator==(const Mylistiterator<T>& InItr)
    {
        return pMy == InItr.pMy;
    }
 
    Mylistiterator& operator=(const Mylistiterator<T>& InItr)
    {
        pMy = InItr.pMy;
        return *this;
    }
 
    Mylistiterator operator++(int)
    {
        Mylistiterator temp = *this;
        ++* this;
        return temp;
    }
 
    Node<T>* pMy;
};
 
template<typename T>
class MyList
{
public:
    MyList()
    {
        Head = new Node<T>();
        Tail = new Node<T>();
 
        Head->pNext = Tail;
        Head->pPrev = nullptr;
        Tail->pPrev = Head;
        Tail->pNext = nullptr;
    }
 
    void push_back(T&& In)
    {
        Node<T>* Temp = new Node<T>();
        Tail->pNext = Temp;
        Temp->pPrev = Tail;
        Tail->data = In;
        Tail = Temp;
    }
    void push_front(T&& In)
    {
        Node<T>* Temp = new Node<T>();
        Head->pPrev = Temp;
        Temp->pNext = Head;
        Head->data = In;
        Head = Temp;
    }
 
    void erase(Mylistiterator<T>& pWhere)
    {
        Node<T>* Temp1 = pWhere->pPrev;
        Node<T>* Temp2 = pWhere->pNext;
        Temp1->pNext = Temp2;
        Temp2->pPrev = Temp1;
        delete pWhere.pMy;
    }
 
    void insert(Mylistiterator<T>& pWhere, T&& data)
    {
        Node<T>* Temp = new Node<T>();
        Node<T>* Temp2 = pWhere->pNext;
        Temp->data = data;
        pWhere->pNext = Temp;
        Temp->pNext = Temp2;
        Temp2->pPrev = Temp;
    }
 
    bool empty()
    {
        return 0 == size();
    }
 
    void clear()
    {
        auto itr = begin();
        while (itr != end())
        {
            erase(itr);
            itr = begin();
        }
    }
 
    int size()
    {
        int cnt = 0;
        auto itr = begin();
        while (itr++ != end())
        {
            ++cnt;
        }
        return cnt;
    }
 
    Mylistiterator<T> begin()
    {
        return Mylistiterator<T>(Head->pNext);
    }
 
    Mylistiterator<T> end()
    {
        return Mylistiterator<T>(Tail);
    }
 
private:
    Node<T>* Head;
    Node<T>* Tail;
};
 
class A {
public:
    A()
        : a(0),
        b(0)
    {
 
    }
 
    A(int _a, int _b)
        : a(_a),
        b(_b)
    {
 
    }
 
    int a;
    int b;
};
int main()
{
    MyList<A> data;
    data.push_back(A(1, 2));
    data.push_back(A(3, 4));
    data.push_back(A(5, 6));
 
    auto it = data.begin();
    ++it;
    data.erase(it);
 
    int Size = data.size();
 
    data.clear();
 
    MyList<char> data2;
    data2.push_back('a');
    data2.push_back('b');
    data2.push_back('c');
 
    for (auto itr = data2.begin(); itr != data2.end(); ++itr)
    {
        cout << (*itr) << endl;
    }
 
    return 0;
}
반응형

'IT > 기타' 카테고리의 다른 글

memo) redis 패턴으로 데이터 삭제  (0) 2021.05.17
stack 구현  (0) 2021.05.17
vector 구현  (0) 2021.05.17
xcode 11에서 cocos2d-x v3 빌드 실패 시 해결 방법  (0) 2020.02.25
프로세스와 스레드의 차이  (0) 2019.12.14
Comments