首页 > 解决方案 > 为什么这个函数会循环?

问题描述

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

我不明白为什么这只是列出打印代码循环。我知道这可能是因为指针不好,但我不知道如何解决它。

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

这是 nodeL 类,GetNext()它只是下一个返回,没有别的。

标签: c++stamp

解决方案


你想next = nullptr在你的构造函数中设置。您还想*&从 getNext 函数返回,如下所示:

nodeL *& getNext();

这两件事应该可以解决您的问题。


推荐阅读