c++中的指针没有显示正确的值

pointer in c++ not showing proper values

本文关键字:显示 指针 c++      更新时间:2023-10-16

我得到了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;
        }
    }
}

我需要对您的链表实现做一些假设。如果我的假设是错误的,那么我的答案也是错误的。

  1. 您正在初始化指向其链表的tail元素的temp指针。典型的命名法是从head开始,向tail发展。此外,in final节点通常将NULL作为其下一个指针。

  2. 您正在将一个链表中的节点与另一个链表上的节点进行比较。链接列表实际上是交叉链接的吗?或者它们实际上是相互独立的?

考虑到这两点,试试这个:

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;
}