Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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++;:运行函数时,程序以无限循环运行_C++_Linked List_Infinite Loop - Fatal编程技术网

C++ C++;:运行函数时,程序以无限循环运行

C++ C++;:运行函数时,程序以无限循环运行,c++,linked-list,infinite-loop,C++,Linked List,Infinite Loop,我已经写了一个函数来合并2个排序的链表。但在我知道我提出的逻辑是否正确之前,我在调用函数时遇到了一个无限循环。我不知道为什么会这样。代码如下: ListNode *merge_sorted_list(ListNode *head1, ListNode *head2) { cout<<"hi\n"; ListNode *curr = new ListNode(-1); ListNode *start = curr; cout<<"hi\n";

我已经写了一个函数来合并2个排序的链表。但在我知道我提出的逻辑是否正确之前,我在调用函数时遇到了一个无限循环。我不知道为什么会这样。代码如下:

ListNode *merge_sorted_list(ListNode *head1, ListNode *head2) {
    cout<<"hi\n";
    ListNode *curr = new ListNode(-1);
    ListNode *start = curr;
    cout<<"hi\n";
    while(head1 || head2) {
        if(head1->val == head2->val) {
            curr->next = head1;
            curr = curr->next;
            curr->next = head2;
            if(head1) head1 = head1->next;
            if(head2) head2 = head2->next;
        } else if(head1->val < head2->val) {
            curr->next = head1;
            curr = curr->next;
            if(head1) head1 = head1->next;
        } else {
            curr->next = head2;
            curr = curr->next;
            if(head2) head2 = head2->next;
        }
    }
    return start->next;

}
ListNode*merge\u sorted\u列表(ListNode*head1,ListNode*head2){
coutnext;
当前->下一步=头2;
如果(head1)head1=head1->next;
如果(head2)head2=head2->next;
}否则如果(表头1->val<表头2->val){
当前->下一步=head1;
当前=当前->下一步;
如果(head1)head1=head1->next;
}否则{
当前->下一步=头2;
当前=当前->下一步;
如果(head2)head2=head2->next;
}
}
返回开始->下一步;
}
我无法在控制台窗口上打印“嗨”。此外,头1和头2不是空值。 请帮忙。
更新:现在可以看到“嗨”。

首先,您正在销毁头1和头2,因为您编写了

curr->next = head1;
后来:

curr->next = head2;
所以你把两者混合在一起,最后你可能会有一些回忆

因此,您希望将两个列表合并为一个新列表。所以试试这个:

ListNode *merge_sorted_list(ListNode *head1, ListNode *head2) 
{
    ListNode* pReturn = new ListNode();
    ListNode* tempRet = pReturn;
    ListNode* temp1 = head1;
    ListNode* temp2 = head2;

    while(temp1 != nullptr || temp2 != nullptr)
    {
        if(temp1 && temp2)
        {
            if(temp1->val < temp2->val)
            {
                tempRet->next = new ListNode(temp1); //the constructor should copy the ListNode value
                tempRet = tempRet->next;
                temp1 = temp1->next;
            }
            else if(temp1->val > temp2->val)
            {
                tempRet->next = new ListNode(temp2);
                tempRet = tempRet->next;
                temp2 = temp2->next;
            }
            else //doubled value
            {
                temp1 = temp1->next;
            }
        }
        else if(temp1)
        {
            while(temp1)
            {
                tempRet->next = new ListNode(temp1);
                tempRet = tempRet -> next;
                temp1 = temp1->next;
            }
        }
        else if(temp2)
        {
            while(temp2)
            {
                tempRet->next = new ListNode(temp2);
                tempRet = tempRet -> next;
                temp2 = temp2->next;
            }
        }
    }
    return pReturn;
}
ListNode*merge\u sorted\u列表(ListNode*head1,ListNode*head2)
{
ListNode*pReturn=新建ListNode();
ListNode*tempRet=pReturn;
ListNode*temp1=head1;
ListNode*temp2=head2;
while(temp1!=nullptr | | temp2!=nullptr)
{
if(temp1和temp2)
{
if(temp1->valval)
{
tempRet->next=newlistnode(temp1);//构造函数应该复制ListNode值
tempRet=tempRet->next;
temp1=temp1->next;
}
否则如果(temp1->val>temp2->val)
{
tempRet->next=新建列表节点(temp2);
tempRet=tempRet->next;
temp2=temp2->next;
}
否则//价值加倍
{
temp1=temp1->next;
}
}
else if(temp1)
{
while(temp1)
{
tempRet->next=新建列表节点(temp1);
tempRet=tempRet->next;
temp1=temp1->next;
}
}
否则,如果(临时2)
{
while(temp2)
{
tempRet->next=新建列表节点(temp2);
tempRet=tempRet->next;
temp2=temp2->next;
}
}
}
返回预烧;
}

请记住,标准输出是行缓冲的,因此如果将其更改为
“hi\n”
,您可能会看到打印输出。谢谢@ThomasPadron McCarthy!我现在能看到“嗨”。但代码仍然无限运行。有什么建议吗?学习使用调试器?如果
NULL
中的某个列表节点为空会怎么样?