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

struct info {
    char nome[50];
};

typedef struct Elemento {
    struct info info;
    struct Elemento *prox;
}Elemento;


Elemento* remove_duplicados(Elemento* agenda)
{
    Elemento* a;
    Elemento* p;
    Elemento* q;

    for(p = agenda; p != NULL; p = p -> prox ){
        a = NULL;

        for(q = p -> prox; q != NULL; ){
            if(0 == strcmp(p -> info.nome, q -> info.nome)){
                if(a == NULL){
                    p -> prox = q -> prox;
                }
                else{
                    a -> prox = q -> prox;
                }

                Elemento* remover = q;
                q = q -> prox;
                free(remover);
            }
            else {
                a = q;
                q = q->prox;
            }
        }
    }

    return agenda;
}

void mostrar_lista(Elemento* agenda){
    Elemento *p;
    for(p = agenda; p != NULL; p = p -> prox){
        printf("%s -> ", p->info.nome);
    }

    printf(" NULL");
}

int main() {
    Elemento *agenda = malloc(sizeof(Elemento));
    strcpy(agenda->info.nome,"ana");
    Elemento *agenda2 = malloc(sizeof(Elemento));
    strcpy(agenda2->info.nome,"ana");
    Elemento *agenda3 = malloc(sizeof(Elemento));
    strcpy(agenda3->info.nome,"carla");
    Elemento *agenda4 = malloc(sizeof(Elemento));
    strcpy(agenda4->info.nome,"vitor");
    Elemento *agenda5 = malloc(sizeof(Elemento));
    strcpy(agenda5->info.nome,"vitor");
    Elemento *agenda6 = malloc(sizeof(Elemento));
    strcpy(agenda6->info.nome,"ana");

    agenda->prox = agenda2;
    agenda2->prox = agenda3;
    agenda3->prox = agenda4;
    agenda4->prox = agenda5;
    agenda5->prox = agenda6;
    agenda6->prox = NULL;

    mostrar_lista(agenda);
	agenda = remove_duplicados(agenda);
	printf("\n\nDuplicados removidos\n\n");
	mostrar_lista(agenda);

	return 0;
}

