algorithm - 弗洛伊德算法 - SIGTSTP 错误

标签 algorithm debugging error-handling floyd-cycle-finding

我正在研究一个问题,以查找给定链表中循环(如果有)中存在的节点数。下面是接受节点头的函数,使用 Floyds 循环算法检查循环,如果找到,则给出循环中的节点数。 在运行程序时,它会给出运行时错误 SIGTSTP,据我所知,这是在程序执行期间停止时传递的信号,考虑到这一点,我无法看到此代码中要更改的内容。在调试时,突出显示的部分似乎是问题的根本原因。

请阐明 SIGTSTP 的含义以及如何在 C++ 中处理它。

int countNodesinLoop(struct Node *head)
{
    Node* slow = new Node;
    Node* fast = new Node;
    slow = head;
    fast = head;

    **do{
        if(slow==NULL || fast==NULL || fast->next==NULL)
            return 0;                            // LOOP NOT FOUND
        slow = slow->next;
        fast = fast->next;
        fast = fast->next;
    }while(slow!=head);**

    // LOOP FOUND
    slow = head;
    while(slow!=fast)
    {
        slow = slow->next;
        fast = fast->next;
    }                      
    // BOTH SLOW AND FAST POINT TO THE NODE WHERE LOOPS START
    int ans = 1;          // COUNTER
    slow = slow->next;                
    while(slow!=fast)
    {
        slow = slow->next;
        ans++;
     }
     return ans;
}

最佳答案

我不知道您为什么会看到 SIGTSTP —— 可能是资源受限环境中的超时?

检查第一个循环中的循环条件。

另外,不要为 slowfast 使用 new 分配空节点。这些是内存泄漏。

关于algorithm - 弗洛伊德算法 - SIGTSTP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703792/

相关文章:

java - 为什么会出现 NullPointerException?

algorithm - 更好地猜测上限

c++ - 如何在调试时显示整个 QString

javascript - 如果文件名太长,在 Firefox 控制台中看不到行号

c++ - 不理解一些编程模式

c# - 用于在 C# 中调试 db 相关方法的更好的错误处理程序代码?

JavaScript 抛出错误 - 没有抛出错误? Mocha 测试问题

algorithm - 为什么计算斐波那契数列的递归方法的时间复杂度是2^n而不是2^n^2?

algorithm - 找到离开顶点的最左边的线

algorithm - 识别二进制文件中的算法