Good afternoon. I'm in trouble with a job I'm doing. The work consists of: creating a student registration system, where the user records the student's name and record. During the listing of the data, the names must be sorted in alphabetical order. The registration and listing part is ok. just the sort order that I can not do.
Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <locale.h>
struct aluno{
int ra;
char nome[20];
struct aluno *anterior;
struct aluno *ant;
int aux;
};
void cadastraaluno();
void listaaluno();
struct aluno *al=NULL, *ant=NULL;
struct aluno aux;
main(){
setlocale(LC_ALL,"portuguese");
int opcao;
do{ // execute o codigo...
system("cls");
printf("============== SISTEMA CADASTRO DE ALUNOS ==============\n\n");
printf("\n 1- Cadastrar");
printf("\n 2- Listar");
printf("\n 3- Sair\n");
printf("\n Qual opção? ");
scanf("%d",&opcao);
switch(opcao)
{// inicio switch
case 1:{
cadastraaluno();
break;
}
case 2:{
listaaluno();
break;
}
case 3:{
printf("Saindo...");
break;
}
default:{
printf("opção inválida.");
}
}//end switch
getch();
}while(opcao!=3); //
getch();
}
void cadastraaluno(){
al=(struct aluno*) malloc(sizeof(struct aluno));
printf("RA:");
scanf("%d",&(*al).ra);
fflush(stdin);
printf("NOME:");
gets((*al).nome);
(*al).anterior=ant;
ant=al;
}
void listaaluno(){
printf("\n Foram cadastrados os seguintes dados: \n\n");
while((*al).anterior !=NULL){ // enquanto registro anterior não for nulo.
printf("\n RA: %d",(*al).ra);
printf("\n NOME: %s\n",(*al).nome);
al=(*al).anterior;
aux=*ant;
*ant=*al;
*al=*ant;
} // termino while
printf("\n RA: %d",(*al).ra);
printf("\n NOME: %s",(*al).nome);
getch();
}
The listing works ok. however it needs to be in alphabetical order. Can someone give me a light?