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

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    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){
                  after_add_node (C, (*A)->value);
                  A= &((*A)->next);
              }
    
              else{
                  A= &((*A)->next);
              }
         }
    
    
    
    
    
         if (((*B)->value)!=NULL) {
    
    
                 if (((*B)->value)%2 == 0){
                      after_add_node (C, (*B)->value);
                      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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,937
    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.
    Last edited by laserlight; 4 Weeks Ago at 06:33 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Thanks a lot Laserlight.
    I try immediatly to verify what you wrote.
    P.S. I agree with you, struct node is better.

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    Quote Originally Posted by laserlight View Post
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,937
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    P.s. I actually needed that clarification. Thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insert a node into the end of a linked list
    By aw1742 in forum C Programming
    Replies: 4
    Last Post: 10-12-2011, 03:50 PM
  2. Implementing STL list::insert
    By sh3rpa in forum C++ Programming
    Replies: 9
    Last Post: 10-23-2007, 12:59 PM
  3. insert sort linked list???
    By vanella*Flavor in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2005, 07:42 PM
  4. Templated List errors with Insert
    By neandrake in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2004, 09:23 PM
  5. Insert in Linked List ...
    By yescha in forum C Programming
    Replies: 4
    Last Post: 11-28-2001, 04:23 AM

Tags for this Thread