简体   繁体   English

气泡式排序一个指针的链表

[英]Bubble sort a linked list with one pointer

I'm trying to bubble sort a linked list with one pointer, p_previous . 我试图用一个指针p_previous冒泡排序一个链表。 The pointer is supposed to look ahead one node and also look ahead two nodes, and if the first is greater than the second, they are to be switched using a temporary variable while p_previous stays out of the swap. 该指针应该向前看一个节点,也向前看两个节点,并且如果第一个大于第二个节点,则将使用一个临时变量来切换它们,而p_previous保持在交换之外。 p_previous should also check down two nodes to see if the list trailer is there, stopping the sort. p_previous还应该检查两个节点以查看列表预告片是否存在,从而停止排序。 I honestly have no clue what I am doing when it comes to bubble sorts, and the linked list implementation isn't helping. 老实说,我不知道在气泡排序方面我在做什么,而且链表的实现也无济于事。

Here is some code: 这是一些代码:

int sort_list(ID_NUMBER *p_list, int list_count)
{
    ID_NUMBER *p_previous = p_list; /* Previous node, use to swap next */
    ID_NUMBER *p_temp;              /* Temporary variable              */
    int count;                      /* Counts number of nodes passed   */

    for(count = 0; count < list_count; count++)
    {
        while(p_previous->p_next_student->p_next_student != NULL)
        {
        if(p_previous->p_next_student->student_id >
                p_previous->p_next_student->p_next_student->student_id)
        {
            p_temp = p_previous->p_next_student->p_next_student;
            p_previous->p_next_student = p_temp->p_next_student;
            p_temp = p_previous->p_next_student;
            p_previous->p_next_student = p_temp;
        }
        p_previous = p_previous->p_next_student;
        }

    }
    return 0;
}

Here is what I know. 这就是我所知道的。

If this is my list as entered. 如果这是我输入的清单。

H-->1-->3-->2-->4-->T H-> 1-> 3-> 2-> 4-> T

1 and 3 are already in order, move p_previous down. 1和3已经按顺序排列,向下移动p_previous

3 and 2 are out of order, make the temp variable point to 2. 3和2乱序,使temp变量指向2。

Make 3 point to the number 4. 使3指向数字4。

Make 2 point to the number 3. 使2指向数字3。

Make 1 point to the number 2. 将1指向数字2。

I think thats how I'm supposed to do it, I just don't know how to put it into code. 我认为这就是我应该怎么做,我只是不知道如何将其放入代码中。 I am pretty sure a while loop inside a for loop inside is all that is necessary. 我非常确定for循环内部的while循环是必需的。

If someone could help, that would be great. 如果有人可以帮助,那就太好了。

Also, if you need more information, just ask. 另外,如果您需要更多信息,请询问。

There's several fundamental problems here ( like you can't change the first item in the list), but the most basic one is your swap: 这里有几个基本问​​题(例如您不能更改列表中的第一项),但是最基本的问题是交换:

        p_temp = p_previous->p_next_student->p_next_student;
        p_previous->p_next_student = p_temp->p_next_student;
        p_temp = p_previous->p_next_student;
        p_previous->p_next_student = p_temp;

So lets say we have three items, we'll call them AB C. And we want AC B. Right now you start off doing: 假设我们有三个项目,我们将其称为ABC。我们想要ACB。现在您开始做:

p_previous = A
p_temp = A->next->next = C

This is going the wrong way. 这是走错路了。 We're going to point A->next at C. The problem is, when we do that we'll lose B. p_temp should store B. 我们将A-> next指向C。问题是,这样做时我们将丢失B。p_temp应该存储B。

p_temp = A->next = B
p_previous->next= p_previous->next->next = C
p_temp->next= p_previous->next->next = {whatever came after}
p_previous->next->next= p_temp = B

Then you just have to deal with the other issues, like crashing when your list has only one item. 然后,您只需要处理其他问题,例如当列表中只有一项时崩溃。

Also you may want to do this: 另外,您可能需要执行以下操作:

for(count = 0; count < list_count; count++)
{
  p_previous = p_list;

So that you go through the list bubbling values N times, instead of just once. 这样您就可以遍历列表冒泡值N次,而不是一次。

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

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