如何将 2 个链表合并或合并在一起以创建新列表
How do I combine or merge 2 linked lists together to create a new list?
我正在尝试获取两个链表"list_1"和"list_2",然后将它们组合并放入"list_3"中。我创建了两个列表,但似乎无法弄清楚如何组合它们。我添加的代码是我创建列表的方式。指针和链表很新,所以任何帮助将不胜感激,谢谢!
struct node
{
int data;
node *next;
};
class List
{
public:
node *head, *tail;
List()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
for(int i = 1; i <= 1; i++)
{
node *temp = new node;
temp -> data = n;
temp -> next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
}
else{
tail -> next = temp;
tail = tail -> next;
}
}
}
你必须"重新连接"它们。 列表 B 的head
应重新连接到列表 A 的tail
,以便可以删除列表 B 的List
对象,但不会删除其成员。引入新的方法merge(List* list)
参数,并将this->tail
重新连接到list->head
,并将this->tail
更新为list->tail
。
如何将 2 个链表合并或合并在一起以创建新列表
只需使用add_node
迭代两个列表即可组合
这里有一个组合修改当前列表的建议,如果您喜欢这些方式,很容易添加新的构造函数在参数或静态方法组合等中获得两个列表
我添加了一些"经典"方法来帮助并能够在没有内存泄漏的情况下使用 valgrind,并且还将列表属性设为私有,因为将它们公开不是一个好方法
#include <iostream>
struct node
{
int data;
node * next;
node(int v) : data(v), next(nullptr) {}
};
class List
{
private:
node *head, *tail;
public:
List() : head(nullptr), tail(nullptr) {}
~List() { clear(); }
List & operator=(const List & l) {
clear();
const node * n = l.head;
while (n != nullptr) {
add_node(n->data);
n = n->next;
}
return *this;
}
// + copy constructor, move etc
void clear() {
while (head != nullptr) {
tail = head->next;
delete head;
head = tail;
}
head = tail = nullptr;
}
void add_node(int n)
{
node * temp = new node(n);
if(head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail -> next = temp;
tail = tail -> next;
}
}
void combine(const List & l1, const List & l2) {
*this = l1;
node * n = l2.head;
while (n != nullptr) {
add_node(n->data);
n = n->next;
}
}
void pr() const {
const node * n = head;
while (n != nullptr) {
std::cout << n->data << ' ';
n = n->next;
}
std::cout << std::endl;
}
};
int main()
{
List l1, l2, l3;
l1.add_node(1);
l1.add_node(2);
l1.add_node(3);
l2.add_node(4);
l2.add_node(5);
l3.add_node(33);
l3.pr();
l3.combine(l1, l2);
l3.pr();
}
编译和执行:
/tmp % g++ -pedantic -Wextra -Wall c.cc
/tmp % ./a.out
33
1 2 3 4 5
瓦尔格林德的处决
/tmp % valgrind ./a.out
==8413== Memcheck, a memory error detector
==8413== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==8413== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==8413== Command: ./a.out
==8413==
33
1 2 3 4 5
==8413==
==8413== HEAP SUMMARY:
==8413== in use at exit: 0 bytes in 0 blocks
==8413== total heap usage: 11 allocs, 11 frees, 176 bytes allocated
==8413==
==8413== All heap blocks were freed -- no leaks are possible
==8413==
==8413== For counts of detected and suppressed errors, rerun with: -v
==8413== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
相关文章:
- C++模板函数中的初始化 - 新的初始值设定项表达式列表被视为复合表达式
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 将新元素添加到列表中,并返回对该元素的引用?
- 将两个列表合并为一个蛇形列表
- 使用基于数组的列表 c++ 合并 2 个数组
- 通过在带有 C++ 的列表中添加连续元素来计算新的整数列表
- 是否可以使用OpenMP并行化一个列表,该列表可以在每次迭代中添加新元素
- C++ 链表合并排序的实现在连接 1 个以上节点的子列表时失败
- 在阵列新表达中的直接限制与列表
- 如何合并两个双重链接列表(访问下一个链接)
- 将 3 个列表合并到一个大列表中
- 合并两个单链接列表以形成第三个列表的功能
- 合并两个排序的列表 - 不起作用
- 将节点添加到链接列表中,而无需明确分配内存空间(无需使用新)
- 添加新节点后,链接列表的负责人总是无效
- 推送功能(将新节点添加到列表顶部)C
- 如何获取两个列表的公共元素并将其存储在新列表中而不会重复
- 如何将 2 个链表合并或合并在一起以创建新列表
- 超过合并 k 排序列表的时间限制(leetcode)
- C++正在将元素复制到新列表中