-
working with two lists
Hey all, I have two problem assignments that I can't seem to get:
here is the first: You are given to linked lists L, and P, containing integers sorted in ascending order. The operation printLots(L, P) will print the elements in L that are specified by P. Write printLots.
this first one I think I figured out, here it is:
Code:
void printLots( list L, list P)
{
for ( int i = 0, P.first(), L.first(); !P; ++P){
i = P() - i; //number of nodes to traverse
for ( int j = 1; (j <= i) && ( !L ); i++, ++L){
if ( !L == 0 )
break;
std::cout << L() << std::endl;
}
}
}
sorry about the sloppy code, but it is a "hand written" assignment. I'm not even sure that it compiles, but I think my logic is right.
Anyhow, here is the second one, where I don't know how to quite start:
---Given two sorted list L1, and L2, write a procedure tp compute L1 (upside down U) L2 using only the basic list operations.
any help will be appreciated,
axon
-
the upside down U is shorthand for union, like in the union of two sets. That means the final list will contain data from both L1 and L2. What you need to decide is whether to create L3 from L1 and L2 or have the final list be a modification of L1 or L2. You also need to decide how to handle the union. Your choices are to do unique widgets, meaning in widget1 is in both L1 and L2 then only one copy of widget1 is in L3, or allow anything goes, in which case if widget1 is in both L1 and L2 then there will be two widget1s in L3.
L1 = 1, 3, 6
L2 = 1, 2, 4
L3 = L1 U L2 (unique) = 1, 2, 3, 4, 6
L3 = L1 U L2 (not unique) = 1, 1, 2, 3, 4, 6
-
thanks, elad, i did figure it out late last night, but was too tired to post...thanks anyway.