Sort a student list by name in alphabetical order

Asked

Viewed 2,329 times

1

That’s what I thought:

Aluno *ordenarNome(Aluno *aluno){
    Aluno *aux;
    Aluno *aux2 = (Aluno*)malloc(sizeof(Aluno));
    Aluno *aux3 = (Aluno*)malloc(sizeof(Aluno));
    Aluno *aux4 = (Aluno*)malloc(sizeof(Aluno));
    Aluno *aux5 = (Aluno*)malloc(sizeof(Aluno));
    *aux = *aluno;
    char nome[30];
    int id = 0;
    int matricula;
    int idade;
    while(aux != NULL){
        *aux2 = *aux->prox;
        while(aux2 != NULL){
            if(aux2->nome[0] < aux->nome[0]){
                *aux3 = *aux2;
                id++;
            }
                aux2=aux2->prox;
        }
        if(id > 0){
            *aux4 = *aux;
            *aux = *aux3;
            while(aux4 != NULL){
                aux->prox=aux4;
                aux5 = aux4->prox;
                if(aux3->id == aux5->id){
                    break;
                }
                aux4=aux4->prox;
            }
        }
        id=0;
    aux=aux->prox;
    }
    aluno = aux;
    return aluno;
}

But it is failing (OBS: compiles using gcc on linux and gives that memory error)

  • Please don’t put tags that have nothing to do with the problem.

  • Prog checks this *aux2 = *aux->Prox; which is possibly putting in the wrong place ;

1 answer

1


Tried using the strcmp() function of the string library. h ?

The function strcmp() takes two string to be compared to each other. The result can have 3 possible returns:

Less than 0: Character of the first String is smaller than the second String.

Equal to 0: When they’re equal.

Greater than 0: When the character of the first String is larger (ASCII code) than of the second String.

Syntax:

strcmp (string1, string2);

Small demonstration:

 #include <stdio.h>
 #include <string.h>//necessário para strcmp
  #include <conio.h>
   int main ()
  {
  char str1[3] = "abc";
  char str2[3] = "abd";
   int retorno;

    retorno = strcmp(str1, str2);
    printf("retorno = %d\n", retorno);
     //mostra o retorno da função strcmp

     getch();
     return 0;
     }
  • it’s giving error and I can’t change the values without losing others, I don’t know how to implode this exchange, all the values are lost in that there.

  • Pupil aux2 = (Student)malloc(sizeof(Student)); there would be no way to do this in the main function? Every time you do this it’s like creating a "node again zeroed" . Do this in main, pass all of them by parameter and then compare.

  • 1

    worked out here buddy, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.