简体   繁体   中英

How to use insertion sort on doubly linked list C++?

I'm working on a project that requires me to sort a linked list using insertion sort, in the most efficient way. I wrote an algorithm that worked but it wasn't the most efficient - it compared values from the beginning of the list instead of going backward. Now, I have an algorithm that compares values going backward, but it doesn't work. The debugger shows that current->prev is a nullptr so it won't run the function. I have it initialized and when I do cout << current->prev it prints the value. I looked at other posts on this topic but I can't find what's wrong with that line of my code. Here's the header file that contains the function in the linked list class:

#include<iostream>

class LinkedList
{
private:
struct ListNode
{
    double value;
    ListNode *next;
    ListNode *prev;
    ListNode(double val, ListNode* nextPtr = nullptr, ListNode* prevPtr = 
nullptr) :
        value(val), next(nextPtr), prev(prevPtr) {}
};
ListNode *head;

public:
LinkedList()
{ 
    head = nullptr; 
}

~LinkedList() 
{
    while (head != nullptr)
    {
        ListNode *current = head;
        head = head->next;
        delete current;
    }
}
void insert(double val)
{
    if (!head)
        head = new ListNode(val);
    else
    {
        ListNode *temp = new ListNode(val);
        temp->next = head;
        head->prev = temp;
        head = temp;
    }
}

void display()
{
    ListNode *temp = head;
    while (temp != nullptr)
    {
    std::cout << temp->value << " ";
    temp = temp->next;
    }
    std::cout << std::endl << std::endl;
}

void insertSort()
{
    ListNode *marker, *current;

    for (marker = head->next; marker != nullptr; marker = marker->next)
    {
        double temp = marker->value;                                 
        current = marker;   

        // this line throws the exception: read access violation.
        // current->prev was nullptr.                                       
        while (current != nullptr && current->prev->value >= temp) 
        {
            current->value = current->prev->value;            
            current = current->prev;                       
        }
        current->value = temp;                  
      }  
   }
};

Here's the source file:

#include<iostream>
#include"Header.h"
using namespace std;

int main()
{
   LinkedList list;

   list.insert(23);
   list.insert(54);
   list.insert(2);
   list.insert(8);
   list.insert(3.2);
   list.insert(14);
   list.insert(43);
   list.insert(0);
   list.insert(9);
   list.insert(2);

   cout << "Contents of linked list before insert sort:\n";
   list.display();


   list.insertSort();

   cout << "Contents of linked list after insert sort:\n";
   list.display();

   return 0;
}

I figured out how to fix this. The inner loop ran while (current != nullptr) so when current was at head and it was assigned current->prev it was pointing to nullptr. I changed the while loop condition to current->prev != nullptr and now it runs properly since it's never pointing to nullptr.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM