working with two lists

This is a discussion on working with two lists within the C++ Programming forums, part of the General Programming Boards category; Hey all, I have two problem assignments that I can't seem to get: here is the first: You are given ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    thanks, elad, i did figure it out late last night, but was too tired to post...thanks anyway.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  3. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  4. help with STL lists
    By WebmasterMattD in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2003, 06:41 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21