简体   繁体   中英

Insertion Sort with a Singly Linked List in C++

I'm trying to write a method for my LinkedList class that will sort a linked list of Person objects by their name. My method compiles fine but when I try to sort a list of people, the output is incorrect. It also never stops running. For example, this code

Person *p1 = new Person("K", "B");
Person *p2 = new Person("A", "A");
Person *p3 = new Person("S", "M");
Person *p4 = new Person("B", "M");

LinkedList ll;
ll.insertFront(*p1);
ll.insertFront(*p2);
ll.insertFront(*p3);
LinkedList newList = ll.insertionSort();
newList.print();
cout << endl;

Gives this output

B, K

A, A

Could anyone help me figure out where I went wrong with my algorithm? Thanks!

This is the method I use to sort names by both first and last:

int Person::compareName(Person p)
{
    if (lName.compare(p.lName) > 0)
    {
        return 1;
    }
    else if (lName.compare(p.lName) == 0)
    {
        if (fName.compare(p.fName) > 0)
        {
            return 1;
        }
        else return -1;
    }
    else return -1;
}

Insertion Sort Method:

LinkedList LinkedList::insertionSort()
   {
    //create the new list
    LinkedList newList;
    newList.front = front;
    
    Node *n;
    Node *current = front;
    Node *trail = NULL;
    
   for(n=front->link; n!= NULL; n = n->link)//cycle through old chain
{
    Node* newNode = n;
    
    //cycle through new, sorted chain to find insertion point
    for(current = newList.front; current != NULL; current = current->link)
    {
        //needs to go in the front
        if(current->per.compareName(n->per) < 0)
        {
            break;
        }
        
        else
        {
            trail = current;
            
        }
    }
    
    //if it needs to be added to the front of the chain
    if(current == front)
    {
        newNode->link = newList.front;
        newList.front = newNode;
    }
    //else goes in middle or at the end
    else{
        newNode->link = current;
        trail->link = newNode;
    }

    return newList;
}

You have current->link in your inner for loop, and in the else to the inner for loop. I assume that you really have current = current->link in the for loop or it does nothing. If so, you'd be skipping every other element.

You also have a language thing- you aren't creating new nodes, you're altering the nodes on your original list. That measn you're changing the list as you walk it, which will corrupt the list as you sort it. Behavior is undefined and dependent on the order in which you add elements.

Even after you have fixed any linked list handling issues (which I haven't looked at), your compareName() function has a flaw - when comparing Person objects that have the same last name it may return from the function without providing a value (in the cases where Name.compare(p.fName) <= 0 ).

Getting an indeterminate result from the compare function will break pretty much any sort.

Since this is likely homework, I'll leave correcting the problem as an exercise.

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