Thread: List exercise: Given two lists, insert in a third list the pair values of...

1. List exercise: Given two lists, insert in a third list the pair values of...

Hi everybody!
I have a little problem with the solution of this problem:
"Given two lists, insert in a third list the pair values of the first one and the odd values of the other one."

My standard list is composed by:

-The nodes, which are structured datas formed by two datas:
a pointer and a float value;
-A pointer, which points to the first node;
-A doublepointer, which points to the pointer.

Here there is my function:

Code:
```void new_pairodd (struct list **A, struct list **B, struct list **C){

if (((*A)->value)!=NULL) {

if (((*A)->value)%2 != 0){
A= &((*A)->next);
}

else{
A= &((*A)->next);
}
}

if (((*B)->value)!=NULL) {

if (((*B)->value)%2 == 0){
B= &((*B)->next);
}

else{
B= &((*B)->next);
}
}
}```
I have a problem with these lines of code:

"if (((*A)->value)!=NULL)"
"if (((*A)->value)%2 != 0)"

Can anyone help me?

P.S. after_add_node is the function which creates new nodes in a list obviously.

2. Assuming that struct list is a badly named node struct, I would simplify to:
Code:
`void new_pairodd(struct list *A, struct list *B, struct list **C){`
The reason is that for the source lists represented by A and B, you only need to read through their values, not modify them, so there is no need for a pointer to a pointer. You might even involve const. However, you do need a pointer to a pointer for C in order to change the head of the list such that it is reflected in the caller. Personally though, I would just work with a pointer to the dynamically allocated head node in the code, then at the end copy it over to *C (or maybe not because it looks like you are abstracting this away with after_add_node). Oh, and I would rename struct list to struct node.

By simplifying, you won't need to write (*A)->value as A->value would be what you want, although you still have to be mindful that you want to compare if the node pointer itself is null, e.g., (((*A)->value)!=NULL) should have been (*A != NULL), which after simplification should be (A != NULL).

On the other hand, if struct list represents the entire linked list (i.e., it has the head pointer as a member, and maybe even a tail member), then C should also be a pointer to struct list. This doesn't seem to correspond to what you described though.

3. Thanks a lot Laserlight.
I try immediatly to verify what you wrote.
P.S. I agree with you, struct node is better.

4. Originally Posted by laserlight
The reason is that for the source lists represented by A and B, you only need to read through their values, not modify them, so there is no need for a pointer to a pointer.
Hi again laserlight.
I didn't understand this.
Are you telling me that, when I use a list, I need a doublepointer only when I have to modify nodes?

5. Originally Posted by letthem
Are you telling me that, when I use a list, I need a doublepointer only when I have to modify nodes?
You need a pointer to a pointer when there is a possibility that you might change the pointer to the head of the linked list such that it ends up pointing to some other node. This is just the usual thing about changing an argument such that it is reflected in the caller: you need a pointer, and in this case your intended argument is a pointer, so your actual argument and hence the formal parameter must be a pointer to a pointer, otherwise you can change the pointer in the function, but that change will not be reflected in the caller.

On the other hand, you can modify a node with only a pointer to the node, and that change will be reflected in the caller.

6. P.s. I actually needed that clarification. Thanks a lot!!!