-
Help please
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
-
I think if you change it as follows it will work
Code:
if (p.getnum() < q.getnum() )
{
unionlist.append(p.getnum());
p.next();
}
else if (p.getnum() > q.getnum())
{
unionlist.append(q.getnum());
q.next();
}
to
if (p.getnum() < q.getnum() )
{
unionlist.append(p.getnum());
p.next();
}
else
{
unionlist.append(q.getnum());
q.next();
}
This way if p.getnum( ) is greater than or equal to q.getnum( ) you append q and get the next q. Since they are equal it does matter which you insert first. The follow iteration p should be less than q and it will be inserted. If not then it is equal (since the lists are sorted) and you'll insert q until p is greater.
-
I don't think I follow your logic.
Aren't you treating p > q the same as p = q? These should be handled as two seperate condtitions.
Maybe I just don't understand where you're going.
Anyways I did try your changes and I now I get an illegal operation error.....
:confused:
-
I think what Dang is getting at is if you have a p.getnum that is equal to a q.getnum then you are either going to insert it before/ after or delete it. Since it appears from your code that you're not deleting it then you may aswell let the greater or less than conditional expression deal with equality depending on your preference.
If this is not the source of your error then it's hard to say where you are going wrong without posting your member function code.