我有一组是在一个链表数字。 我想对它们进行比较,看看它们是一组相同的号码。 这里是我的代码现在:
bool set::equalset(set second)
{
Data *travel1, *travel2;
travel1 = top;
travel2 = second.topval(); //gets the top value for the second set
while (travel2->next != NULL && travel1->next != NULL)
{
if (!in(travel2->value)) //in checks if the value is in the set
return false;
if (!second.in(travel1->value)) //same idea here
return false;
travel2 = travel2->next;
travel1 = travel1->next;
return true;
}
return false;
}
所以,我的代码确实是抓住了这两个组的前值,并设置这些分别等于travel1 / 2,然后在旅游不指向在这两套空值,它遍历列表,并检查是否从任一值该套是在彼此。 如果没有找到一个值,将其设置为false。 否则,将被设置为true,他们被发现是相等的。
然而,这个代码只有一半的工作 - 你可以很容易地通过输入1,2为第二组中的第一组和1,2,3打破它,他们将返回相等。 我认为第三个值(3)将使其返回false。 什么是这里缺少的环节?
你的代码中有几个问题。 首先,你不检查的最后一个节点。 一个循环条件如下:
while (travel2->next != NULL && travel1->next != NULL)
将尽快打破的统计员的一个到达最后一个节点,但从未对其进行检查。 此外,这也将意味着两套单 -node,每个总是比较真实的。
接下来,你只有一次迭代后硬回车,所以这个不断上具有相同节点值开始两套返回false没有想到的办法。
travel2 = travel2->next;
travel1 = travel1->next;
return true; // this doesn't belong here.
接下来,您通过值的参数,这意味着被调用拷贝构造函数。 我不知道你是否实现与否(如果你没有,你有一个完整的不同的问题),但没有理由重复名单只是为了看它是否等于*this*
。 该函数应采取一个const引用作为参数。
bool set::equalset(const set& second)
最后,你的退出条件是正确的,但你不能假定名单均被耗尽。 你必须验证它。 如果任旅行者非空你可以通过返回false做到这一点(和他们中的一个将是如果列表是不平坦的。
全部放在一起:
bool set::equalset(const set& second)
{
const Data *travel1 = top;
const Data *travel2 = second.top;
while (travel1 && travel2)
{
if (!in(travel2->value)) //in checks if the value is in the set
return false;
if (!second.in(travel1->value)) //same idea here
return false;
travel2 = travel2->next;
travel1 = travel1->next;
}
return !(travel1 || travel2);
}
优化排序的列表
如果你保持在输入和拆卸方法排序的列表,你可以显著让下面这个更容易,可见:
bool set::equalset(const set& second)
{
const Data *travel1 = top;
const Data *travel2 = second.top;
while (travel1 && travel2 && travel1->value == travel2->value)
{
travel1 = travel1->next;
travel2 = travel2->next;
}
return !(travel1 || travel2);
}
在你所描述的情况,进行搜索前第二组的第三个元素,打破while循环的第一个集合的迭代器将是NULL。 您可以通过每个以及循环独立设置。 你也可以检查,如果两组都比较每个元素之前的相同数量的元素。
您需要调整您的收益状况,因为截至目前,如果你在第一值返回true, this
和second
存在于每个列表。
做这样的事情,而不是:
bool set::equalset(set second)
{
Data *travel1, *travel2;
travel1 = top;
travel2 = second.topval(); //gets the top value for the second set
while (travel2->next != NULL && travel1->next != NULL)
{
if (!in(travel2->value)) //in checks if the value is in the set
return false;
if (!second.in(travel1->value)) //same idea here
return false;
travel2 = travel2->next;
travel1 = travel1->next;
}
return true;
}
与你的代码,如果两套有不同数量的成员。 你的循环将退出作为travel1
或travel2
具有较少没有。 元素将指向NULL
,其它的时候仍然没有NULL
。 在你的情况travel1
将指向NULL
,并仍将有要素的解析travel2
下面的代码通过检查
bool set::equalset(set second)
{
Data *travel1, *travel2;
travel1 = top;
travel2 = second.topval(); //gets the top value for the second set
while (travel2->next != NULL && travel1->next != NULL)
{
if (!in(travel2->value)) //in checks if the value is in the set
return false;
if (!second.in(travel1->value)) //same idea here
return false;
travel2 = travel2->next;
travel1 = travel1->next;
}
if (travel2->next == NULL && travel1->next == NULL)
{
return true;
}
return false;
}
在此代码为2的元素组1 2 2和1 2它将返回假