Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LinkedList中的C Bubblesort_C_Pointers_Linked List_Swap - Fatal编程技术网

LinkedList中的C Bubblesort

LinkedList中的C Bubblesort,c,pointers,linked-list,swap,C,Pointers,Linked List,Swap,我正在尝试对双链接列表进行排序,但遇到了一些问题。我是C语言的noob,我想我的问题是指针 我只是不知道如何在列表中交换两个位置,所以这可能就是问题所在 我试着用Bubblesort对它进行排序,即使知道它的复杂性不是很好,因为我仍然在学习,认为这是一个简单的开始 我还试着阅读一些关于在linkedlist中交换元素以及如何对它们进行排序的内容,但我真的被这个问题困住了 PS:我用m->next启动for,因为我的列表有一个标题(m) PS2:我收到一个错误“请求非结构或联合中的成员‘next’

我正在尝试对双链接列表进行排序,但遇到了一些问题。我是C语言的noob,我想我的问题是指针

我只是不知道如何在列表中交换两个位置,所以这可能就是问题所在

我试着用Bubblesort对它进行排序,即使知道它的复杂性不是很好,因为我仍然在学习,认为这是一个简单的开始

我还试着阅读一些关于在linkedlist中交换元素以及如何对它们进行排序的内容,但我真的被这个问题困住了

PS:我用m->next启动for,因为我的列表有一个标题(m)

PS2:我收到一个错误“请求非结构或联合中的成员‘next’”,不知道如何修复它

struct segment {
   int x, y;   /// position
   char c;   // letter 
   struct segment* next;
   struct segment* prev; 
};


void sortingSegments(struct segment* m) {
   struct segment **j; struct segment **i;

   for(i = &((m->next)->next); i !=NULL; i = i->next) {
          for(j = &(m->next); j == i; j = j->next) {
              if ((*j)->c > (*i)->c) {
                  struct segment **aux;
                  aux = i;
                  (*aux)->next = (*i)->next;
                  (*aux)->prev = (*i)->prev;

                  i = j;
                  (*i)->next = (*j)->next;
                  (*i)->prev = (*j)->prev;

                  j = aux;
                  (*j)->prev = (*aux)->prev;
                  (*j)->next = (*aux)->next;
              }
          }
   } 
}

请阅读评论并尝试理解节点的链接

它是基于


你的问题是指针。您必须传递列表的地址,而不仅仅是指向它的指针(
m
),以处理第一个节点改变位置和列表地址改变的情况。因此,您需要对段进行排序(struct segment**m),然后只需使用
段*j*i
并调整间接寻址的剩余级别。在交换第一个节点的情况下,不要忘记设置
*m=new\u first\u node\u地址或您的列表将在排序后中断。
void sortingSegments(struct segment** m) {
    struct segment *i, *tmp, *prev, *next;
    int swapped = 1;
    /*
     * https://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation
     *  n = length(A)
     *  repeat
     *    swapped = false
     *    for i = 1 to n-1 inclusive do
     *      //if this pair is out of order
     *      if A[i - 1] > A[i] then
     *        // swap them and remember something changed
     *        swap(A[i - 1], A[i])
     *        swapped = true
     *      end if
     *    end for
     *  until not swapped
     */

     // looping until no more swaps left
     while (swapped) { 
        swapped = 0;
        // we begin from the second item at each iteration
        for (i = (*m)->next; i; i = i->next) { 
            // we need to swap i with i->prev
            if (i->prev->c > i->c) { 
                prev = i->prev;
                next = i->next;
                // swapping first and second elements,
                // so update m to reflect the change made 
                // to the head of the list
                if (prev == *m) { 
                    *m = i;
                } 
                // so there is a prev of prev, update that two links
                else { 
                    prev->prev->next = i;
                    i->prev = prev->prev;
                }
                // so there is a next, update that two links
                if (next) { 
                    next->prev = prev;
                    prev->next = next;
                }
                // no next element, mark the end of the list
                else { 
                    prev->next = NULL;
                }
                // this is obvious, prev now becomes i's next
                prev->prev = i; 
                i->next = prev;

                // this is needed to reflect the change in i
                i = prev; 
                swapped = 1;
            }
        }
    }
}