2017-07-09 4 views
1

Ich versuche, einen Code zu machen, um eine verknüpfte Liste zu sortieren den Zeiger Wert ändern.Das Programm stürzt ab, wenn ich zum letzten Knoten komme. Die searchMinor Funktionen funktioniert gut, das Problem ist in der Logik von Sortierliste.Sortieren einer verknüpften Liste Ändern der Werte des Zeigers

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

Ihre Codierung Stil ist schrecklich, wussten Sie, dass der Compiler Whitespaces ignoriert? Menschen nicht. Deshalb schreiben wir nicht: * Yourcodingstyleirribly, hast du dieCompilerignoreswhitespaces? * –

+1

't_list * min = & n, * l = & n;': '& n' ist die lokale Zeigeradresse. – BLUEPIXY

Antwort

0

Sie den Erhalt der lokalen Kopie von nbegin in sortList. Da Sie eine lokale Kopie zurückgeben, ist dies der Grund für undefiniertes Verhalten. Stattdessen müssen Sie die Adresse übergeben. Wie,

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

// and call like in sortList 
searchMinor(&nbegin) 
Verwandte Themen