【问题标题】:C Bubblesort in LinkedListC 链表中的冒泡排序
【发布时间】:2016-03-29 04:41:02
【问题描述】:

我正在尝试对双向链表进行排序,但遇到了一些问题。我是 C 的菜鸟,我想我的问题是指针..

我只是看不到如何交换列表中的两个位置,所以也许这就是问题所在。

我尝试使用 Bubblesort 对其进行排序,即使知道它的复杂性并不是那么好,因为我仍在学习,认为这是一种简单的开始方式。

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

PS:我用 m->next 开始 for,因为我的列表有一个 header(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) 来处理第一个节点更改位置和列表地址更改的情况。所以你需要sortingSegments(struct segment **m),然后你可以简单地使用segment *j, .. *i并调整剩余的间接级别。如果第一个节点被交换,请不要忘记设置*m = new_first_node_address;,否则您的列表将在排序后中断。

标签: c pointers linked-list swap


【解决方案1】:

请阅读 cmets 并尝试理解节点的链接。

它基于simple bubble sort described in wikipedia

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;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2011-04-13
    • 2012-09-29
    • 2018-05-18
    相关资源
    最近更新 更多