I have a homework assignment that calls for me to read in two lists of sorted integers and then find the Union, Intersection, etc...
I'm using a simple class based linked list approach.
I'm kind of embarrased to admit this but I've run into a problem that I just can't seem to solve!
The problem comes when the two lists are equal I get an infinte loop somewhere in my UnionList function and I can't seem to figure out why.
Here's the code I have for said function:
// FINDUNION FUNCTION /////////////////////////////////////////////////////////////
void FindUnion()
{
p.setfirst();
q.setfirst();
int control = 1;
while (control == 1)
{
if (p.getnum() < q.getnum() )
{
unionlist.append(p.getnum());
p.next();
}
else if (p.getnum() > q.getnum())
{
unionlist.append(q.getnum());
q.next();
}
else if (p.getnum() == q.getnum())
{
unionlist.append(p.getnum());
if (!p.atend() && !q.atend())
{
p.next();
q.next();
}
}
if (p.atend() && q.atend())
{
if (p.getnum() < q.getnum() )
{
unionlist.append(q.getnum());
}
else if (p.getnum() > q.getnum())
{
unionlist.append(p.getnum());
}
else if (p.getnum() == q.getnum())
{
unionlist.append(p.getnum());
}
control = 0;
break;
}
} // end while
cout << endl << "UNION:" << endl;
cout << "(";
unionlist.printlist();
cout << ")" ;
} // END FIND UNION ///////////////////////////////////////////////
a lot of encapulated class member function type stuff but you should be able to get the basic idea.
I must have a logic error somewhere.... I'd appreciate any suggestions.
One last thing. It seems to be able to handle lists as long as the last #'s aren't the same. I.E. 2467 and 2 4 6 8 work fine, while 2 4 6 8 and 2 4 6 8 produce the infinite loop.
Thanks