两个链表的联合 - C++
Union of two linked list- C++
任何帮助都会有所帮助。我写了一个代码来查找两个链表的并集。但是,我在一个部分得到了无限循环。我在代码中指出它。请帮助我识别错误。谢谢。
//Finds the union of two linked lists.
nodeType* unionLL(nodeType *&headA, nodeType *&headB)
{
nodeType *tempA, *tempB, *newNode;
tempA = headA;
tempB = headB;
bool match = false;
while (tempB -> link != NULL)
{
while (tempA -> link != NULL)
{
outfile <<"The infinite loop occurs here " << endl;
if (tempB -> intVal == tempA -> intVal)
{
match = true;
}
tempA = tempA -> link;
}
if (!match)
{
newNode = new nodeType;
newNode -> intVal = tempB -> intVal;
newNode -> link = NULL;
tempA -> link = newNode;
}
tempA = headB;
tempB = tempB -> link;
}
return headB;
}
您尚未确定链表是否排序 - 因此我们应该假设没有。 您尚未确定可以修改哪个列表;从表面上看,函数可以修改这两个列表。 您返回headB
,这表明结果应该是可从headB
访问的结果集应包含该列表中的每个元素,以及可从headA
访问的尚未通过headB
找到的每个元素的新元素。
从表面上看,你的伪代码应该是:
foreach element in listA
if (element not found in listB)
add copy of element to listB
留下列表 A 原封不动。 您的代码未实现此逻辑。
我认为您没有检查它是否在 tempA 循环中匹配(在 B 中也没有)
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 链表,将列表复制到另一个列表
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 对单向链表进行排序时出现运行时错误