简体   繁体   English

双向链表的插入排序 C

[英]Insertion sort of a doubly linked list in C

I am trying to implement the insertion sort algorithm in a doubly linked list, here is my code:我正在尝试在双向链表中实现插入排序算法,这是我的代码:

void insertion_sort_list(listint_t **list)
{
        listint_t *head, *insert, *temp, *aux;

        head = *list;
        while (head)
        {
                insert = head->next;
                aux = head;
                while (aux && aux->n > insert->n)
                {
                        temp = aux->next->next;
                        aux->next = temp;
                        temp->prev = aux;
                        aux = aux->prev;
                }
                temp = aux->prev;
                insert->next = aux;
                insert->prev = temp;
                head = head->next;
        }
}

I have tried out the code but it gives me an infinite loop.我已经尝试了代码,但它给了我一个无限循环。 When I am try to run it in my debugger it, I couldn't understand it (My executable is compiled from multiple files).当我尝试在我的调试器中运行它时,我无法理解它(我的可执行文件是从多个文件编译而来的)。 Can some one help find out where my error is, and how can I find these kind of errors in the future(infinite loops)?有人能帮我找出我的错误在哪里吗,我以后怎么能找到这类错误(无限循环)? And if possible can you refer me to a documentation, or anything, that will help me use the gdb to debug an executable compiled from multiple C files.如果可能的话,你能给我推荐一份文档或任何东西,这将帮助我使用gdb调试从多个 C 文件编译的可执行文件。 Thank you.谢谢你。

I am going to answer my own question.我要回答我自己的问题。 So this problem has made me spend nearly two days trying to figure out how I can implement the insertion sort algorithm in a doubly linked list.所以这个问题让我花了将近两天的时间想弄清楚如何在双向链表中实现插入排序算法。 I do not like copying other people code, because why would I learn if I do.我不喜欢抄别人的代码,因为我抄了干嘛要学。

Here is how I solved my issue:这是我解决问题的方法:

void insertion_sort_list(listint_t **list)
{
        listint_t *sorted, *curr, *next, *aux, *temp;

        sorted = NULL;
        curr = *list;

        while (curr != NULL)
        {
                next = curr->next;

                if (sorted == NULL || sorted->n > curr->n)
                {
                        curr->next = sorted;
                        sorted = curr;
                } else
                {
                        aux = sorted;

                        while (aux->next != NULL && aux->next->n <= curr->n)
                                aux = aux->next;

                        if (aux->next == NULL)
                        {
                                aux->next = curr;
                                curr->prev = aux;
                                curr->next = NULL;
                        } else
                        {
                                temp = aux->next;
                                aux->next = curr;
                                curr->prev = aux;
                                curr->next = temp;
                                temp->prev = curr;
                        }
                }

                curr = next;
        }

        *list = sorted;
}

As you can see, instead of traversing backward I made a dummy head so as I can make a sorted part of the list.如您所见,我没有向后遍历,而是制作了一个虚拟头部,这样我就可以对列表进行排序。 This approach is much cleaner than my previous one.这种方法比我以前的方法干净得多。 I have understood that algorithms are hard, and implementing them takes time.我明白算法很难,实现它们需要时间。 And I, having only implemented bubble sort before, have taken at least two days two come up with a solution.而我,之前只实现了冒泡排序,至少花了两天时间想出了一个解决方案。 But it will make you understand more about the concept.但它会让你对这个概念有更多的了解。

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

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