【发布时间】:2018-06-04 00:55:12
【问题描述】:
所以这是我的排序功能,我不是交换链表中的信息,只是交换下一个节点的指针。我有一个类似的功能,可以按完美运行的汽车价格排序,当我使用按汽车年份排序时,我得到分段错误 11。
void ordenaCrescenteAno(ELEMCAR *iniLista){
ELEMCAR *aux1 = NULL;
ELEMCAR *aux2 = NULL;
ELEMCAR *maior = NULL;
ELEMCAR *troca = NULL;
if(iniLista == NULL){
printf("Lista Vazia\n");
return;
}
for(aux1 = iniLista; aux1 != NULL; aux1 = aux1 -> seguinte){
maior = aux1;
for(aux2 = aux1; aux2 != NULL; aux2 = aux2 -> seguinte){
if(aux2->info.ano > maior->info.ano){
maior = aux2;
}
}
if(maior != aux1){
troca->seguinte = aux1->seguinte;
aux1->seguinte = maior->seguinte;
maior->seguinte = troca->seguinte;
}
}
}
我将把链表的详细信息放在这里:
typedef struct carro{
char matricula[7];
char marca[30];
char modelo[30];
int ano;
char classe[30];
float preco;
char combustivel[10];
int dataInspecao;
int dataRevisao;
char observacoes[100];
int estado;
}CARINFO;
typedef struct CarElem{
CARINFO info;
struct CarElem *seguinte;
}ELEMCAR;
我做错了什么?我想不通,因为我有一个类似的功能,但我是按“preco”变量排序的。
【问题讨论】:
标签: c sorting pointers linked-list