递归函数在运行时崩溃

KOB*_*KOB -3 c++ recursion linked-list

我正在编写一个涉及链表的程序.我编写了一个函数,它返回链表中的第n个节点,它以递归方式调用自身.我的程序编译并运行直到递归函数然后崩溃.这是节点的构造函数以及递归函数:

LinkedList::LinkedList():
    head(head){
        sizeInt = 0;
}

Node* LinkedList::get_nth(const int& n) const {
    Node* node = new Node();
    for(int counter = 1; counter <= n; counter++){
        node = get_nth(counter + 1);
    }
    return node;
 }
Run Code Online (Sandbox Code Playgroud)

这个功能有什么问题?如果您需要更多详细信息或代码,请告诉我们.

Bat*_*eba 5

没有什么可以阻止递归(其中,有一个n递增调用增加到n + 1.)

这会溢出你的堆栈,程序将因此而崩溃.