C++ 交替合并列表

C++ 交替合并列表,c++,data-structures,linked-list,C++,Data Structures,Linked List,在下面的代码中,我尝试交替合并2个列表,而不是按相反的顺序打印它们。但是我的代码没有给出正确的输出,它只是合并了第二个列表的最后一个元素。 输入: 一, 三, 1 3 5 三, 2 4 6 实际产量: 5 6 3 1 预期产出: 5634112 有人能告诉我我的代码有什么问题吗 #include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node *next; }; void

在下面的代码中,我尝试交替合并2个列表,而不是按相反的顺序打印它们。但是我的代码没有给出正确的输出,它只是合并了第二个列表的最后一个元素。 输入:

一,

三,

1 3 5

三,

2 4 6

实际产量: 5 6 3 1

预期产出: 5634112

有人能告诉我我的代码有什么问题吗

#include<bits/stdc++.h>
using namespace std;
struct Node
{
    int data;
    struct Node *next;
};
void push(struct Node ** head_ref, int new_data)
{
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
void printList(struct Node *head)
{
    struct Node *temp = head;
    while (temp != NULL)
    {
        cout<<temp->data<<' ';
        temp = temp->next;
    }
    cout<<' ';
}
void mergeList(struct Node **head1, struct Node **head2);

int main()
{
    int T;
    cin>>T;
    while(T--){
        int n1, n2, tmp;
        struct Node *a = NULL;
        struct Node *b = NULL;
        cin>>n1;
        while(n1--){
            cin>>tmp;
            push(&a, tmp);
        }
        cin>>n2;
        while(n2--){
            cin>>tmp;
            push(&b, tmp);
        }
        mergeList(&a, &b);
        printList(a);
        printList(b);
    }
    return 0;
}

void mergeList(struct Node **p, struct Node **q)
{
     struct Node*temp1=*p,*temp2=*q,*t1,*t2;
     while(temp1!=NULL)
     {
         if(temp2==NULL)
            break;
         t1=temp1->next;
         t2=temp2;
         temp1->next=t2;
         t2->next=t1;
         temp1=t1;
         *q=temp2->next;
         temp2=*q;
     }
 }
#包括
使用名称空间std;
结构体类型
{
int数据;
结构节点*下一步;
};
无效推送(结构节点**head\u ref,int new\u数据)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建_节点->数据=新建_数据;
新建节点->下一步=(*head\u ref);
(*head_ref)=新节点;
}
无效打印列表(结构节点*头)
{
结构节点*温度=头部;
while(temp!=NULL)
{
coutn1;
而(n1-){
cin>>tmp;
推送(&a,tmp);
}
cin>>n2;
而(n2-){
cin>>tmp;
推送(&b,tmp);
}
合并列表(&a和&b);
印刷品清单(a);
印刷品清单(b);
}
返回0;
}
无效合并列表(结构节点**p,结构节点**q)
{
结构节点*temp1=*p,*temp2=*q,*t1,*t2;
while(temp1!=NULL)
{
if(temp2==NULL)
打破
t1=temp1->next;
t2=temp2;
temp1->next=t2;
t2->next=t1;
temp1=t1;
*q=temp2->next;
temp2=*q;
}
}

老实说,我真的不确定您在
合并列表
功能中到底在做什么。这段代码相当危险,所以我没有冒昧地验证它的正确性。我已经重命名了一些变量并重新编写了代码,所以您可以将其作为参考点,看看代码有什么问题

    void mergeList(struct Node **p, struct Node **q)
    {
     struct Node *a = *p, *b = *q, *next_a, *next_b;
     while(a != NULL)
     {
         if(b == NULL)
            break;

         next_a = a->next;
         a->next = b;
         next_b = b->next;
         b->next = next_a;
         a = next_a;
         b = next_b;

     }
 }

希望这有帮助。干杯。

你能解释一下预期的输出吗?他只是在用链表来填充列表,所以列表的最后是
1 3 5
2 4 6
,按相反的顺序从每次迭代中提取一个字符。如果是这样的话,那么应该是6 5 4 3 2 1还是5 6 3 4 1 2?我已经更正了it@AndrewScott你能告诉我吗为什么代码没有显示正确的输出a我从您的代码中发现了错误,因此我相应地更正了代码。注意,由于您正在将
b
合并到
a
,因此无需传递指针的地址。列表地址永远不会更改-您只需重新连接指针
void mergeList(结构节点*p,结构节点*q)
很好,不需要额外的
a
b
指针。(您可以使用
p
q
进行迭代)。另外,
while(a!=NULL)
不考虑
b
有更多节点的列表。如果(!b)中断,则在(a | | b)时更好地
;else if(!a){a=b;break;}else{/*您的代码*/}
但是,逻辑方面做得很好。值得一提的UV。备选方案示例:(链接过期:2019年5月18日星期六02:46:01 CDT)感谢David的建议。注意到了。