C Experts: Please help with link list coding 
Author Message
 C Experts: Please help with link list coding

We were given problems to code in our intro to C,
can someone help us with our coding in link list,
which we are not very good at.

1) Write a recursive function findfirst() to find the first
occurrence of a data element on link list1 which matches
on link list2.  The function need only to return the address
reference.  For example, using the link lists below, the
return value would be an address of the node in list1
which contains the 16.  A sample call is shown below.

list1 --->[32] --->[41] ---->[16] ---->[25]--->x

list2 --->[23] --->[25] --->[11] ---->[16]

      node * np=findfirst(list1, list2)
[Hint: You may want to to implement this
as two recursive function, not one]

2)  Normally, a link list has node which are homogeneous,
that is, all of the same type.  Suppose however that we
wanted to have a linked list with two types of heterogeneous
nodes.  Foe example, the list might be bank customer, some
depositing into accounts, some just cashing checks.

list -->[acct 100]--->[cash ck] --->[cash ck] -->[acct 105] --->[cash ck]-
->
            [add 50.0]     [200.00   ]       [70.00     ]      [sub 60.0]
      [25.00     ]

a) Define such a node to enable the implemation of this list.

b) Write a list traversal function which would print out the
     list.  Note that add means deposit and sub means
    withdrawal.

Any help and hint would be very appreciated.  Thanks.
We have to hand this in Friday(5/3/96).

Tom and Jim.



Wed, 14 Oct 1998 03:00:00 GMT  
 C Experts: Please help with link list coding

Correction to the look on problem 2.

We were given problems to code in our intro to C,
can someone help us with our coding in link list,
which we are not very good at.

1) Write a recursive function findfirst() to find the first
occurrence of a data element on link list1 which matches
on link list2.  The function need only to return the address
reference.  For example, using the link lists below, the
return value would be an address of the node in list1
which contains the 16.  A sample call is shown below.

list1 --->[32] --->[41] ---->[16] ---->[25]--->x

list2 --->[23] --->[25] --->[11] ---->[16]

      node * np=findfirst(list1, list2)
[Hint: You may want to to implement this
as two recursive function, not one]

2)  Normally, a link list has node which are homogeneous,
that is, all of the same type.  Suppose however that we
wanted to have a linked list with two types of heterogeneous
nodes.  Foe example, the list might be bank customer, some
depositing into accounts, some just cashing checks.

list -->[acct 100]--->[cash ck] --->[cash ck] -->[acct 105] --->[cash
ck]
           [add 50.0]      [200.00   ]       [70.00     ]      [sub 60.0]
      [25.00     ]

a) Define such a node to enable the implemation of this list.

b) Write a list traversal function which would print out the
     list.  Note that add means deposit and sub means
    withdrawal.

Any help and hint would be very appreciated.  Thanks.
We have to hand this in Friday(5/3/96).

Tom and Jim.



Wed, 14 Oct 1998 03:00:00 GMT  
 C Experts: Please help with link list coding

: 1) Write a recursive function findfirst() to find the first
: occurrence of a data element on link list1 which matches
: on link list2.  The function need only to return the address

Try restating the problem

        findfirst(list1,list2)
                if first element of list1 is found is list2,
                        return the address of the element (on list1 or list2?)
                if not found,
                        findfirst on the rest of list1 and list2
                (what if list1 is empty?)

        find(element,list2)
                if list2 is empty, not found
                if the first element of list2 matches element, found it
                else find the element in the rest of list2

: 2)  Normally, a link list has node which are homogeneous,
: that is, all of the same type.  Suppose however that we
: wanted to have a linked list with two types of heterogeneous

Sounds like a job for a tagged union. A union can be used as any other
element of a structure:

        enum kind {car,boat};
        struct {
                enum kind vehicleKind;
                union {
                        int numDoors;
                        int displacement;
                } property;
        } X;

X can refer to either a boat or car. If you want to be a boat,
        X.vehicleKind = boat;
and then only refer to the displacement property,
        X.property.displacement.

If later X is to be car, you should change the tag
        X.vehicleKind = car;
before referring to
        X.property.numDoors

(The language does not define any relation between such a
discriminating union tag and which member of the union is
currently defined. It is up to the programmer to show
discipline.)

: a) Define such a node to enable the implemation of this list.

You can link together structs with union members just the same
way as any other type of member.

: b) Write a list traversal function which would print out the
:      list.  Note that add means deposit and sub means
:     withdrawal.

If the nodes contain tagged unions (tag member and a union
member), you can distinguish which kind of node you're looking
at during the traversal.
--

the Queen who straits, the Queen of strife;|          Cupertino, California
with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
she brings the choice of birth or knife.   |         I don't use no smileys



Thu, 15 Oct 1998 03:00:00 GMT  
 C Experts: Please help with link list coding


Quote:

>We were given problems to code in our intro to C,
>can someone help us with our coding in link list,
>which we are not very good at.

[snip]

Quote:
>Tom and Jim.

What is this??  Do my homework week?

Ross



Fri, 16 Oct 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Learning Linked Lists, Code Attached, My understanding of code attached, Please confirm understanding

2. Factory Code (Experts please help!)

3. Link List to Link List, HELP friends

4. DOUBLY LINKED LIST, EXPERTS!!!

5. Please Help: Sorting a linked list

6. Please I need some help -- traversing trough a doubly linked list--

7. Help Please!(linked lists/dynamic memory allocation etc.)

8. Please help with C++ linked list problem

9. Linked List-Queues/Stacks PLEASE HELP

10. PLEASE HELP-I NEED LINKED LIST GURUS!

11. Doubly Linked Lists...HELP PLEASE (sorry if this a repost)

12. Help please, Linked list problem

 

 
Powered by phpBB® Forum Software