Thread: Problem transferring nodes between linked lists

  1. #1
    Registered User
    Join Date
    Mar 2012

    Problem transferring nodes between linked lists

    Hi C wizards,
    I am working on a demographic simulation model where I have a population of individual people represented as structs:

    struct person
       int age;
       struct person *next_node;
    The data to populate the model is read in from a csv file and the individuals are added to one of three linked lists based on their age: child, adult, and elderly. The lists are sorted in descending order so that the oldest person in the group is the head node and the youngest person is always the tail node.

    On each day of the simulation, the age of each person is incremented and when a threshold age is reached, the individual needs to be transferred into the next group. For example, this is the code snippet that transfers a child to the adult list. The function add_person_to_list() inserts the node based on age, which in this particular case, will always be at the end of the list.

     int threshold = 7000;
      struct person *prev_node;
     struct person *current_node;
     prev_node = child;
     current_node = child;
     while(current_node != NULL)
         if(current_node -> age < threshold)
             current_node -> age = (current_node -> age) + 1;
             prev_node = current_node;
             current_node = current_node -> next_node;
             current_node -> age = (current_node -> age) + 1;
             child = current_node -> next_node;
             prev_node -> next_node = current_node -> next_node;
             current_node -> next_node = NULL; /* PROBLEM LINE !!!*/
             adult = add_person_to_list(current_node, adult);
             current_node = prev_node -> next_node; /* ALSO BECOMES NULL */
    The problem I am running into is setting the "next_node" pointer to NULL when the node is transferred to the new list. The way my current code is set up, the pointer for "prev_node -> next_node" is also changed to NULL and the while loop terminates without reading through the end of the child list.

    I tried creating a dummy pointer and having 'next_node' point at that instead, but it doesn't seem to work.

     struct person *dummy_ptr = NULL;
     current_node -> next_node = dummy_ptr;
    I also tried copying the node before transfer. This works, but it seems like a recipe for massive memory leaks.

    struct person *copy_node( struct person *original_node)
       struct person *new_node = (struct person*)malloc(sizeof( struct person )); 
       new_node -> age = original_node -> age;
       new_node -> next_node = NULL;
      current_node -> age = (current_node -> age) + 1;
      child = current_node -> next_node;
      prev_node -> next_node = current_node -> next_node;
      new_node = copy_node(current_node);
      adult = add_person_to_list(new_node, adult);
      current_node = prev_node -> next_node;
    I am probably missing something intuitive about pointers and memory. What method would you guys suggest?

    The real simulation model is a lot more complex in terms of the decisions and individuals that have to be transferred. Hopefully this is enough information for you to understand my problem. Thanks in advance for your help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    No offence, but this is like an old elementary school reading comprehension test:

    A green train and a yellow train leave St. Louis travelling 70mph. The green train is carrying wheat to Chicago, the yellow train is carrying 650 passengers to San Fransisco. Both trains run on diesel. Joe is driving the yellow train. How far will the green train have travelled after two hours?
    Most of the information is irrelevant to the question.

    You want to delete a node from one linked list, and add a node to another linked list, correct? Then just do it. Presumably, you can already add nodes. If you are unsure how to delete nodes, ask about deleting nodes (or find an explanation online).
    Last edited by MK27; 04-07-2012 at 03:43 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. linked lists, ways for adding nodes
    By nik2 in forum C Programming
    Replies: 37
    Last Post: 04-20-2010, 02:56 AM
  3. moving nodes between linked lists
    By budala in forum C Programming
    Replies: 0
    Last Post: 08-09-2009, 08:52 AM
  4. Transferring text from file into linked list
    By manutdfan in forum C Programming
    Replies: 41
    Last Post: 01-22-2007, 06:19 AM
  5. Logic problem adding nodes to linked list
    By SeanMSimonsen in forum C++ Programming
    Replies: 0
    Last Post: 04-02-2003, 07:04 PM