简体   繁体   English

在C ++中使用选择排序对链表进行排序

[英]Sorting a linked list using selection sort in C++

I'm running into some sort of runtime error when trying to test my sort method. 尝试测试我的排序方法时,我遇到了某种运行时错误。 With my implementation, I attempt to find the smallest node in a linked list... Following that I test if the smallest node is first node, last node, or just in the middle. 通过我的实现,我尝试在链接列表中找到最小的节点...之后,我测试最小的节点是第一个节点,最后一个节点还是仅在中间。 After testing for these cases, I then attempt to add the smallest value into a new linked list. 在测试了这些情况之后,我然后尝试将最小值添加到新的链表中。 I do this into all the values are sorted, then point head(private variable in my class) to the newly sorted list... If I need to include my header file or anything else, please just let me know. 我对所有值进行排序,然后将head(类中的私有变量)指向新排序的列表...如果我需要包括头文件或其他内容,请告诉我。 Any help is appreciated. 任何帮助表示赞赏。

To be clear, there's no actual error message, the program just gets terminated when I call my sort function. 需要明确的是,没有实际的错误消息,该程序只是在我调用sort函数时终止。

void Linkedlist::sort()
{
    Node * current = head;
    Node * smallest = head;
    Node * newHead = NULL;
    Node * newTail = NULL;

    while(head != NULL)
    {
        current = head;
        while(current != NULL)
        {
            if(current->elem < smallest->elem)
            {
                smallest = current;
            }
            current = current->next; 
        }

        //smallest is first node
        if(smallest->prev == NULL)
        {
            head = head->next;
            head->prev = NULL;
        }

        //smallest is last node
        else if(smallest->next == NULL)
        {
            tail = tail->prev;
            tail->next = NULL;
        }

        else
        {
            smallest->prev->next = smallest->next;
            smallest->next->prev = smallest->prev;
        }

        //adding smallest to a new linked list
        if(newHead == NULL)
        {
            smallest->prev = NULL;
            smallest->next = NULL;
            newHead = smallest;
        }
        else
        {
            smallest->prev = newTail;
            smallest->next = NULL;
            newTail->next = smallest;
            newTail = smallest;
        }
    }
    //point head to new linked list
    head = newHead;

}

You need to set newTail when adding the first element to the new linked list. 将第一个元素添加到新的链接列表时,需要设置newTail

Otherwise when newTail->next = smallest is executed for the second new entry, it will be a null pointer access. 否则,当对第二个新条目执行newTail->next = smallest ,它将是空指针访问。

Just add 只需添加

newTail = smallest

after

newHead = smallest

after adding 添加后

newTail = smallest

when putting the smallest element into the first node, and adding 将最小的元素放入第一个节点并添加时

smallest = head

to the top of my outer while loop, I still was running into an infinite loop. 在我的外部while循环的顶部,我仍然遇到无限循环。 I figured out that for one, I needed to point tail to the newTail at the end by saying 我想通了一点,我需要说尾巴指向newTail

tail = newTail

After that, my function still caused a segfault. 在那之后,我的功能仍然引起了段错误。 This segfault was due to me trying to access the prev member of a NULL. 此段错误是由于我试图访问NULL的prev成员而引起的。

head = head->next //head is now NULL
head->prev = NULL //segfault

This situation occurred when there was only one node was left in the unsorted list. 当未排序列表中只剩下一个节点时,就会发生这种情况。 I fixed this issue by adding an if statement inside of the if statement that checks if the smallest is the first node. 我通过在if语句中添加一个if语句来解决此问题,该语句检查最小的是否是第一个节点。 The if statement inside checked if it was also the final node(aka the last node remaining) 内部的if语句检查它是否也是最后一个节点(又称剩余的最后一个节点)

//smallest is first node
        if(smallest->prev == NULL)
        {
            //check if smallest is the ONLY node left
            //if it is only node left, head = head->next makes head NULL 
            //                         so then head->prev = NULL causes segfault
            if(smallest->next == NULL)
            {
                //break leaves while loops 
                //which is why you have to add the final node 
                //outside of the while loops
                break;
            }

            else
            {
                head = head->next;
                head->prev = NULL;

            }
        }

When there is one node remaining, it will hit the break, and exit both while loops. 当剩下一个节点时,它将中断并退出两个while循环。 This means the final node was never added to the sorted list. 这意味着最终节点从未添加到排序列表中。 To fix this I just used the following code before pointing head and tail to their new list. 为了解决这个问题,我只是在将头和尾指向他们的新列表之前使用了以下代码。

smallest->prev = newTail;
smallest->next = NULL;
newTail->next = smallest;
newTail = smallest;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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