Search code examples
c++stamp

Why is this function going in a loop?


void listord::stmp()
{
    nodeL *aux=head;
    cout<<"STMP"<<endl;
    while (aux)
    {
        cout<<aux->getData();
        aux=aux->getNext();
    }
    cout<<endl;
    return;
}

I can't understand why this simply list print code loops. I know that it could be because of a bad pointer but i don't know how to solve it.

class nodeL{
    private:
        int data;
        nodeL *next;
    public:
        nodeL();
        nodeL(int d);
        nodeL(int d,nodeL *n);
        int getData();
        nodoL *getNext();
        void setData(int d);
        void setNext(nodeL *n);
};

This is the nodeL class, the GetNext() it's just a return next, nothing else.


Solution

  • You want to set next = nullptr in your constructor. Also you want to return *& from your getNext function, like this:

    nodeL *& getNext();
    

    These two things should fix your issue.