2016-03-29 10 views
-2

二重にリンクされたリストをソートしようとしていますが、何か問題があります。私はCでnoobだと私は私の問題は、ポインタと思われる...C Bubblesort in LinkedList

私はちょうどリスト内の2つの位置を交換する方法を見ることはできませんと多分それは問題です。

私はBubblesortを使ってそれを並べ替えることを試みました。それは複雑さがそれほど良くないことを知っていても、私はまだ学習しているので、簡単に始めることができたと思ったからです。

私ものLinkedListの要素をswapingとどのようにそれらを並べ替えるために約いくつかのものを読んでtryiedが、私は本当にこの問題にこだわって...

PS:私はとのために始めたM->次の私のリストにはヘッダー(m)があります。

PS2:私は「何かない構造体または共用で、 『次へ』メンバーの要求」エラーを取得しています、そしてそれは

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

あなたの問題はポインタです。最初のノードが位置を変更し、リストアドレスが変更された場合を処理するために、リストのアドレス( 'm')だけでなく、リストのアドレスを渡す必要があります。だからあなたは 'sortingSegments(struct segment ** m)'が必要です。単純に 'segment * j、.. * i'を使い、間接指示の残りのレベルを調整することができます。最初のノードがスワップされた場合は、 '* m = new_first_node_address;'を設定することを忘れないでください。そうしないと、ソート後にリストが破損します。 –

答えて

0

のコメントを読んでみてください修正する方法がわかりませんノードのリンクを理解する。

これは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; 
      } 
     } 
    } 
}