c++中的指针没有显示正确的值
pointer in c++ not showing proper values
我得到了3个链表和并集函数
a
b
和result是我想用元素填充结果列表的列表,但它总是空的。
主要是result.UnionSets(a,b)
功能是
void UnionSets(linkedlist & l1, linkedlist & l2)
{
node<type> *temp= l1.tail;
if(temp!=NULL)
{
while(temp->next!=tail)
{
AddNode(temp->data);
temp=temp->next;
}
}
temp=l2.tail;
if(temp!=NULL)
{
while(temp->next!=tail)
{
AddNode(temp->data);
temp=temp->next;
}
}
}
我需要对您的链表实现做一些假设。如果我的假设是错误的,那么我的答案也是错误的。
-
您正在初始化指向其链表的
tail
元素的temp
指针。典型的命名法是从head
开始,向tail
发展。此外,in final节点通常将NULL作为其下一个指针。 -
您正在将一个链表中的节点与另一个链表上的节点进行比较。链接列表实际上是交叉链接的吗?或者它们实际上是相互独立的?
考虑到这两点,试试这个:
void UnionSets(linkedlist & l1, linkedlist & l2)
{
node<type> *temp= l1.head;
while(temp!=NULL)
{
AddNode(temp->data);
temp=temp->next;
}
temp=l2.head;
while(temp!=NULL)
{
AddNode(temp->data);
temp=temp->next;
}
}
类似的东西?
static linkedlist Union(linkedlist& A, linkedlist& B)
{
linkedlist result;
for(linkedlist::iterator iter = A.begin(); iter != A.end(); ++iter)
{
result.append(*iter);
}
for(linkedlist::iterator iter = B.begin(); iter != B.end(); ++iter)
{
result.append(*iter);
}
return result;
}
相关文章:
- 为什么将指针重新分配给 1D 数组的 char 时显示错误,但 2D 数组工作正常?
- 使用指针显示数组的值
- 为什么我无法显示指针的地址?
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 可视C++打印指针无法确认显示基础
- 错误:通过指针显示重复的结果
- 使用指针时,C 显示向量
- 使用全局变量显示C++在使用 pthread 时比指针慢 100%?
- Visual Studio 中的"指向引用的指针"错误不会按发生位置的文件名显示
- 当我要修改指针到常量整数时,为什么我的编译器不显示错误
- 类型限定符出现在多项声明中(用指针显示奇怪的行为)
- fn 指针的向量没有显示正确的大小()
- 如何将结构指针插入stl向量中并显示内容
- 访问唯一指针变量并显示它们
- 定义指针时不显示输出;
- 遍历指针数组并进行显示
- 为什么由指针和对象构建的局部变量显示不同的输出
- 当处理指针时,控制台中为*p1和p显示的不同值的含义
- 类数据成员内存布局显示指针大小过大
- Visio & UML - 在属性和返回值中显示指针