gpt4 book ai didi

c++ - 如何合并(连接)2 个列表?

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:52 25 4
gpt4 key购买 nike

这是我用于连接列表的代码,但由于某种原因,在它这样做之后它以循环结束并且不会停止。

  void concatenate(){
p2 = first2;
while (p2 != NULL) {
p = new list;
p->data = p2->data2;
last->next = p;
last = p;
p2 = p2->next2;
}
}

data - 第一个列表的信息
data2 - 第二个列表的信息
last - 来自第一个列表的指针
p - 来自第一个列表的指针
p2 - 来自第二个列表的指针

最佳答案

 void concatenate(){

last->next = first2; // done

}

//if not the same type or cannot be casted


void concatenate(){
if ( first2 == null ){
last->next = 0;
return;
}

last->next = new list(first2->data2); //connect the lists
first2=first2->next;
last=last->next;

while( first2->next ) // copy the values
{
last->next = new list(first2->data2);
first2=first2->next;
last=last->next;

}
last->next=0;
}

关于c++ - 如何合并(连接)2 个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43822378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com