insert names neatly into a list, I'm not knowing fz this

/ / function to insert ai; ai so does it with the first 3 names, the others it does not sort

Aluno *cad(Aluno *aluno)
{
  Aluno *aux;
  while(1)
  {
    aux = aluno;
    if(aux->prox == NULL)
    {
        Aluno *criar = novo();
        if(criar == NULL)
        {
            break;
        }
        if(strcmp(aux->nome, criar->nome) > 0)
        {
            criar->prox = aux;
            aux = criar;
        }
        else
        {
            aux->prox = criar;
        }
    }
    else
    {
        Aluno *criar = novo();
        if(criar == NULL)
        {
            break;
        }
        Aluno *aux2;
        Aluno *aux3 = aux;
        while(aux3->prox != NULL)
        {
            printf("1\n");
            aux2 = aux3->prox;
            if(strcmp(criar->nome, aux3->nome) > 0)
            {
                printf("entrei no if\n");
                criar->prox = aux2;
                aux3->prox = criar;
                break;
            }
            aux3 = aux3->prox;
        }
        if(aux3->prox == NULL)
        {
            aux3->prox = criar;
        }
    }
    aluno = aux;
}
  return aluno;
}
Author: rafael marques, 2018-05-23

1 answers

I am assuming that there exists a function Aluno* criar().

Try This Here:

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

Aluno* criar(); // prototipo da funcao ja existente

Aluno* inserir(Aluno* alunoAInserir, Aluno* lista)
{
    Aluno* a = lista;
    Aluno* anteA = a;
    int cmpAnterior = 9;
    int cmpAtual;
    int inserido = FALSE;

    if (a == NULL)
    {
        a = lista = criar();
        return inserir(alunoAInserir, lista);
    }

    for (; a != NULL; anteA = a, a = a->prox)
    {
        if ((cmpAtual = strcmp(alunoAInserir->nome, a->nome)) != cmpAnterior)
        {
            if (cmpAnterior != 9)
            {
                if (cmpAtual < 0)
                {
                    // Insere a esquerda.
                    alunoAInserir->prox = anteA->prox /*que e o proprio a*/;
                    anteA->prox = alunoAInserir;
                }
                else if (cmpAtual == 0)
                {
                    // Insere a direita.
                    alunoAInserir->prox = a->prox;
                    a->prox = alunoAInserir;
                }

                if (cmpAtual <= 0)
                {
                    inserido = TRUE;
                    break;
                }
            }
            else if (cmpAnterior == 9 && cmpAtual < 0)
            {
                // Insere na cabeca da lista.
                Aluno* tmp = lista;
                lista = alunoAInserir;
                alunoAInserir->prox = tmp;

                inserido = TRUE;

                break;
            }

            cmpAnterior = cmpAtual;
        }
    }

    if (!inserido)
    {
        // Insere no fim da lista.
        anteA->prox = alunoAInserir;
        alunoAInserir->prox = NULL;
    }

    return lista;
}

Function usage:

Aluno* lista = inserir(alunoAInserir, NULL);
 2
Author: Marcelo Shiniti Uchimura, 2018-05-26 14:31:13