Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序在实现链表时挂起_C++_Linked List - Fatal编程技术网

C++ 程序在实现链表时挂起

C++ 程序在实现链表时挂起,c++,linked-list,C++,Linked List,本课程的目标是更好地理解链表。我应该创建函数PromoteTarget,它执行以下操作: 浏览数字列表,如果未找到指定的数字(目标),则会将其追加到列表的末尾。如果列表为空,则target将成为head 如果在列表中找到目标一次或多次,则将目标编号置于列表的前面。我应该能够做到这一点,而无需更改或删除任何节点 这是我的密码: void PromoteTarget(Node*& headPtr, int target) { Node *current =

本课程的目标是更好地理解链表。我应该创建函数PromoteTarget,它执行以下操作:

  • 浏览数字列表,如果未找到指定的数字(目标),则会将其追加到列表的末尾。如果列表为空,则target将成为head
  • 如果在列表中找到目标一次或多次,则将目标编号置于列表的前面。我应该能够做到这一点,而无需更改或删除任何节点
这是我的密码:

      void PromoteTarget(Node*& headPtr, int target)
    {

    Node *current = headPtr;
    Node *prevnode = headPtr;
    int count = 0;
    while (current != 0) {
        if (current->data == target) {
            count++;
        }
        current = current->link;
    }
    if (count == 0) {

        Node *newNodePtr = new Node;
        newNodePtr->data = target;
        newNodePtr->link = 0;
        if (headPtr == 0)
            headPtr = newNodePtr;
        else
        {
            Node *cursor = headPtr;

            while (cursor->link != 0)
                cursor = cursor->link;
            cursor->link = newNodePtr;
        }
    }

    if (count > 0) {
        current = headPtr;
        while (current != 0) {
            if (current->data == target) {
                prevnode->link = current->link;
                current->link = headPtr;
                headPtr = current;
            }
            prevnode = current;
            current = current->link;
        }
    }
    }
这是一个我应该作为输出接收的示例:

================================
passed test on empty list
================================
initial: 4  5  
(target: 3)
ought2b: 4  5  3  
outcome: 4  5  3  
================================
initial: 3  4  5  7  4  5  
(target: 5)
ought2b: 5  5  3  4  7  4  
outcome: 5  5  3  4  7  4  
================================
initial: 7  6  4  3  4  5  
(target: 4)
ought2b: 4  4  7  6  3  5  
outcome: 4  4  7  6  3  5  
================================
initial: 6  5  7  5  3  5  6  5  3  
(target: 7)
ought2b: 7  6  5  5  3  5  6  5  3  
outcome: 7  6  5  5  3  5  6  5  3  
================================
initial: 5  
(target: 6)
ought2b: 5  6  
outcome: 5  6  
================================
initial: 5  6  4  4  5  7  
(target: 6)
ought2b: 6  5  4  4  5  7  
outcome: 6  5  4  4  5  7  
================================
这就是我实际得到的:

================================
passed test on empty list
================================
initial: 4  5  
(target: 3)
ought2b: 4  5  3  
outcome: 4  5  3  
程序挂起,从未给我完整的输出。根据我得到的结果,我对我的程序的前半部分按预期工作有一个很好的感觉。虽然我可能完全错了。
如果我不得不猜测,每当我尝试将元素移动到链表的前面时,我的问题就会出现。我的逻辑看起来正常吗?我是否忘记或遗漏了什么,或者我只是犯了一个愚蠢的错误?

您可以在一次遍历链表的过程中执行这两个操作。在遍历过程中,每次遇到
数据
等于
目标
的节点时,将该节点移动到链表的开头,并将布尔变量
exists
设置为true。在遍历结束时,如果
存在==false
,则在链表的末尾添加一个新节点

Node* createNode(int target)
{
    Node *newNodePtr = new Node;
    newNodePtr->data = target;
    newNodePtr->link = nullptr;
    return newNodePtr;
}

void PromoteTarget(Node*& headPtr, int target)
{
    if (headPtr == nullptr)
        headPtr = createNode(target);
    else
    {
        Node *prev, *curr;
        prev = headPtr;
        curr = headPtr->link;
        bool exists = false;
        while (curr != nullptr)
        {
            if (curr->data == target)
            {
                exists = true;
                prev->link = curr->link;
                curr->link = headPtr;
                headPtr = curr;
                curr = prev->link;
            }
            else
            {
                prev = curr;
                curr = curr->link;
            }
        }

        if (exists == false)
            prev->link = createNode(target);
    }
}

当程序挂起时,使用调试器将其中断。它说它在哪一行?所显示的代码不会产生任何输出,这使得我们很难根据您所述的症状进行调试。我建议您缩小问题发生的范围,并根据从调试中学到的知识创建一个循环。一种可能是您有一个无限循环(循环列表),但我们无法知道,因为我们不知道如何构建列表。通过使用调试器或在上述函数中添加一些输出都应该可以工作。@JosephSible如果我正确使用调试器,那么我认为它挂在函数的最后一行。@JosephSible“程序在PromoteTarget中接收到信号SIGINT,Interrupt.0x000000000041D3E(headPtr=@0x7fffffebc8:0x615c80,target=5)位于llcpImp.cpp:284 current=current->link