#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Lista lista;
struct Lista {
char *nome;
lista *prox;
};
void adicionar(lista **l, char *nome) {
lista
*novo
= malloc(sizeof(lista
)); novo->prox = *l;
*l = novo;
}
void ordenar(lista **l) {
if(*l == NULL || (*l)->prox == NULL) return; //se for nulo(vazio), ou apenas 1 elemento
lista *aux = *l, *t;
char s[100]; //precisa de espacao suficiente para armazenar o nome
while(aux != NULL) {
t = aux->prox;
while(t != NULL) {
if(strcmp(aux
->nome
, t
->nome
) > 0) { //se vir depois }
t = t->prox;
}
aux = aux->prox;
}
}
void exibir(lista *l) {
while(l != NULL) {
printf("%s %s", l
->nome
, (l
->prox
) != NULL
? ", " : "]"); l = l->prox;
}
}
int main()
{
lista *l = NULL;
adicionar(&l, "ana");
adicionar(&l, "joao");
adicionar(&l, "leo");
adicionar(&l, "beto");
adicionar(&l, "katia");
ordenar(&l);
exibir(l);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdHlwZWRlZiBzdHJ1Y3QgTGlzdGEgbGlzdGE7CgpzdHJ1Y3QgTGlzdGEgewogICAgY2hhciAqbm9tZTsKICAgIGxpc3RhICpwcm94Owp9OwoKdm9pZCBhZGljaW9uYXIobGlzdGEgKipsLCBjaGFyICpub21lKSB7CgogICAgbGlzdGEgKm5vdm8gPSBtYWxsb2Moc2l6ZW9mKGxpc3RhKSk7CiAgICBub3ZvLT5ub21lID0gbWFsbG9jKHN0cmxlbihub21lKSArIDEgKiBzaXplb2YoY2hhcikpOwogICAgc3RyY3B5KG5vdm8tPm5vbWUsIG5vbWUpOwogICAgbm92by0+cHJveCA9ICpsOwogICAgKmwgPSBub3ZvOwp9Cgp2b2lkIG9yZGVuYXIobGlzdGEgKipsKSB7CgogICAgaWYoKmwgPT0gTlVMTCB8fCAoKmwpLT5wcm94ID09IE5VTEwpIHJldHVybjsgLy9zZS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