2017-07-09 3 views
1

私はリンクされたリストをソートするコードを作成しようとしています。最後のノードに到達するとプログラムがクラッシュします。searchMinor関数はうまく動作します。 sortList。リンクされたリストを並べ替えるポインタの値を変更する

typedef struct{ 
    int num; 
}t_dataL; 

typedef struct s_nodeL{ 
    t_dataL dataL; 
    struct s_nodeL *next; 
}t_nodeL; 

typedef t_nodeL *t_list; 

t_list *searchMinor(t_nodeL*n){ 
    t_list *min=&n, *l=&n; 
    l=&(*l)->next; 
    while(*l) { 
     if((*l)->dataL.num < (*min)->dataL.num)min=l; 
     l=&(*l)->next; 
    } 
    return min; 
} 

void sortList(t_list *l){ 
    t_nodeL *nbegin=*l;   //save the beginig of the list in nbegin 
    t_list *plec,*aux; 
    plec=searchMinor(nbegin); //search the minor from the begining 
    *l=*plec;     //put as the first elemnet on the list the fisrt minor value 
    while(nbegin->next){ 
     aux=&(*plec)->next; //Save the value of the next of the minor in aux 
     *plec=*aux;   //remove the minor from the list 
     plec=searchMinor(nbegin); //search the new minor from the begining of the list 
     *aux=*plec;   //the next of the old minor is the new minor 
    } 
} 
+1

あなたのコーディングスタイルはひどいです、あなたはコンパイラーが空白を無視することを知っていますか?人間はしません。私たちが書いていない理由は次のとおりです。*あなたのコーディングスタイルは、コンパイラが白い空白を認識しましたか?* –

+1

't_list * min =&n、* l =&n;': '&n'はローカルポインタアドレスです。 – BLUEPIXY

答えて

0

あなたはsortListnbeginのローカルコピーを取得しています。ローカルコピーを返すので、の未定義の動作の理由です。代わりに、あなたはそれのアドレスを渡す必要があります。同様に、

// edit the function according to the signature 
t_list *searchMinor(t_nodeL **n); 

// and call like in sortList 
searchMinor(&nbegin) 
関連する問題