dimanche 30 juin 2019

simple linked list iterator

Hi i have a simple linked list.

I want to know how i would make a simple iterator for it.

Im not sure how th do this, would really love the help.

I want to be able to call it like this

    SList<class RandomClass> simpleList;
    for (auto i : simpleList)
    {

    }

but at the moment i call it like this

        SList<class RandomClass> simpleList;
        for (SLNode<RandomClass>* node = simpleList.Head(); node; node = node->Next())
        {

        }

Here is the list class

template<class T> class SList;
template<class T>
class SLNode
{
public:
    friend class SList<T>;
    SLNode<T>* NodeNext;
    T* NodeData;

    SLNode<T>* Next() const
    {
        return (SLNode<T>*)NodeNext;
    }

    T* Data() const
    {
        return (T*)NodeData;
    }
};


template<class T>
class SList
{
public:
    SLNode<T>* HeadNode;
    SLNode<T>* TailNode;

    SLNode<T>* Head() const
    {
        return HeadNode;
    }

    SLNode<T>* Tail() const
    {
        return TailNode;
    }
};

Aucun commentaire:

Enregistrer un commentaire