我试图制作一个代码来对链接列表进行排序,从而对指针值进行排序。当我到达最后一个节点时程序崩溃.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
}
}
答案 0 :(得分:0)
您正在nbegin
中获取sortList
的本地副本。因为您要返回本地副本,所以这是未定义行为的原因。相反,你必须传递它的地址。像,
// edit the function according to the signature
t_list *searchMinor(t_nodeL **n);
// and call like in sortList
searchMinor(&nbegin)