Thread: Double Linked List Code Explain

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    2

    Exclamation Double Linked List Code Explain

    Hello to everyone.
    Im trying to learn some more things about the linked lists.
    I have here two structures Users and Ratings the ratings is double linked list and link to users.

    Can someone explain me exactly what they do every line...i have many things in my mind about, but i want to clear everything from an expert
    Code:
    USER *user = usertable[slot];
    (user->num_ratings)++;          // here is a counter
    RATING_NODE *prev = NULL;
    RATING_NODE *curr = user->ratings;
    while (curr != NULL)
    {
    if (rnode->item_id < curr->item_id)
      break;                        // ok here checking the input item value with existing in the nodes
    prev = curr;                    // search serialise every next
    curr = curr->next;
    }
    
    if (prev == NULL) {
      rnode->next = user->ratings;
      user->ratings->prev = rnode;
      user->ratings = rnode;
    } else if (curr == NULL) {
      prev->next = rnode;
      rnode->prev = prev;
    } else {
      prev->next = rnode;
      rnode->prev = prev;
      rnode->next = curr;
      curr->prev = rnode;
    }
    
    }

    Thanks everyone for your time!.
    Last edited by Salem; 03-30-2022 at 12:50 PM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    424
    Code:
    
    USER *user = usertable[slot];            // Find our user based on slot #
    (user->num_ratings)++;                   // Increase the number of ratings
    RATING_NODE *prev = NULL;                // We are going to keep track of the previous item, so we can do the insert
    RATING_NODE *curr = user->ratings;       // Start at the head of the user's 'ratings' list
    
    
    // We are going to walk the list until either
    // * The current nodes's item_id is greater than the rating we are adding
    // * We reach the end of the list
    while (curr != NULL)                     // While we have not reached the end of the list
    {
        if (rnode->item_id < curr->item_id)  // If we have reahed the point where we want to insert
          break;                             // then drop out of the loop
    
    
        prev = curr;                         // Move onto the next item in the list
        curr = curr->next;
    }
    
    
    // We now have three different places to insert
    // - At the start of the list
    // - In the middle of the list
    // - At the end of the list
    // It seems somebody has forgotten the 4th case - if the list is completely empty...
    if (prev == NULL) {                      // Was prev never assigned anything but the initial NULL?                                         
       rnode->next = user->ratings;          // We are inserting at the head of the list
       user->ratings->prev = rnode;          // THIS LINE SEEM WRONG.. a NULL pointer access if user->ratings is NULL???
       user->ratings = rnode;
    } else if (curr == NULL) {               // Did we walk off the end of the list? 
       prev->next = rnode;                   // Insert at the end of the list - need to update 2 pointers
       rnode->prev = prev;
    } else {                                 // Both prev and curr are not NULL, so we are in the middle of the list
       prev->next = rnode;                   // Insert into the middle of the list - need to update 4 pointers
       rnode->prev = prev;
       rnode->next = curr;
       curr->prev = rnode;
    }

  3. #3
    Registered User
    Join Date
    Mar 2022
    Posts
    2
    Thank you Hamster.
    You clear my thoughts, fortunately they follow your instructions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-03-2014, 09:49 AM
  2. Dynamic List Char Array & Double Linked List
    By Roadrunner2015 in forum C Programming
    Replies: 18
    Last Post: 10-20-2013, 01:31 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. Can someone explain Linked List to me?
    By Frost Drake in forum C++ Programming
    Replies: 22
    Last Post: 01-03-2006, 08:32 PM
  5. Sort strings in double linked list. (Optimize code)
    By netstar in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2005, 01:40 AM

Tags for this Thread