Using insertion sort on a singly linked list

前端 未结 7 1123
春和景丽
春和景丽 2021-02-06 10:11

So I have an assignment where I\'m giving a random list of number and I need to sort them using insertion sort. I must use a singly linked list. I looked around at other posts b

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 11:00

    Wow, I'm sure late to the party huh? Regardless, I would like to throw in my two cents in answering this question. Based on the way you provided your sample code, I am going to assume the following:

    1. You are not using OOP principles
    2. You already have the singly linked list setup correctly (I'll refer to it as forward list)
    3. You want the user to use operator '=' when calling the implementation function

    To begin, the answer I'm about to give you uses what I like to call the double pointer technique! Where you use a double pointer to help you reference two variables at the same time such that you avoid a complicated test case when reaching nullptr.

    Okay, okay... I am playing fast in lose by calling pointer-to-pointer double pointer, and I may be tugging your leg by calling it a technique! If you have mastery over the concepts of pointers, then what I am about to show you should be quite trivial. I call pointer-to-pointer a technique, because rarely anybody talks about it or uses it in their example source code! The following link will take you to a blog created by Semmy Purewal that discusses this concept in more detail!

    forward_list *insertion_sort( forward_list *head )
    {
      /// Ensure provided linked list is not empty and has more than one element...
      if( head == nullptr || head -> next == nullptr )
      {
        std::cout << "Error: empty or single element linked list provided!\n";
        return;
      }
    
      /// Initialize separate variable the houses a growing sorted output...
      forward_list *sorted = nullptr;
    
      /// Traverse provided singly linked list...
      while( head != nullptr )
      {
        /// Remember current head before popping it off...
        forward_list *current_head = head;
    
        /// Double pointer for effective splicing of unsorted singly linked list...
        forward_list **trail = &sorted;
    
        /// Now we pop off the current head...
        head = head -> next;
    
        /// Iterate through our growing sorted output to figure out where we need to the head...
        while( ( *trail ) != nullptr && ( *trail ) -> data > current_head -> data )
        {
          /// Head does not belong in this current position, move on!
          trail = &( *trail ) -> next;
        }
    
        /// Insert the head and update our growing sorted output to point to the head...
        current_head -> next = ( *trail );
        ( *trail ) = current_head;
      }
    
      /// Return our sorted output...
      return sorted;
    }
    

    There we go, you now have an example implementation of insertion sort that operates wonderfully on a singly linked list! This implementation does not have a messy test case for nullptr, because we used a double pointer named trail that keeps track of itself and the pointer pointing to our growing sorted output. Thus, as the algorithm iterates through the sorted output, the variable trail gets dereferenced at nullptr, which holds the last node's next pointer!

    All in all, I hope you find this answer to be both helpful and a little bit more readable! I know this question was asked seven years ago and was only active one year ago, but I hope someone finds this somewhat useful. Whelp, I'll be on my way then!

提交回复
热议问题