#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;
}
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); }
}
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;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKc3RydWN0IGluZm8gewogICAgY2hhciBub21lWzUwXTsKfTsKCnR5cGVkZWYgc3RydWN0IEVsZW1lbnRvIHsKICAgIHN0cnVjdCBpbmZvIGluZm87CiAgICBzdHJ1Y3QgRWxlbWVudG8gKnByb3g7Cn1FbGVtZW50bzsKCgpFbGVtZW50byogcmVtb3ZlX2R1cGxpY2Fkb3MoRWxlbWVudG8qIGFnZW5kYSkKewogICAgRWxlbWVudG8qIGE7CiAgICBFbGVtZW50byogcDsKICAgIEVsZW1lbnRvKiBxOwoKICAgIGZvcihwID0gYWdlbmRhOyBwICE9IE5VTEw7IHAgPSBwIC0+IHByb3ggKXsKICAgICAgICBhID0gTlVMTDsKCiAgICAgICAgZm9yKHEgPSBwIC0+IHByb3g7IHEgIT0gTlVMTDsgKXsKICAgICAgICAgICAgaWYoMCA9PSBzdHJjbXAocCAtPiBpbmZvLm5vbWUsIHEgLT4gaW5mby5ub21lKSl7CiAgICAgICAgICAgICAgICBpZihhID09IE5VTEwpewogICAgICAgICAgICAgICAgICAgIHAgLT4gcHJveCA9IHEgLT4gcHJveDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgYSAtPiBwcm94ID0gcSAtPiBwcm94OwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIEVsZW1lbnRvKiByZW1vdmVyID0gcTsKICAgICAgICAgICAgICAgIHEgPSBxIC0+IHByb3g7CiAgICAgICAgICAgICAgICBmcmVlKHJlbW92ZXIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgYSA9IHE7CiAgICAgICAgICAgICAgICBxID0gcS0+cHJveDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gYWdlbmRhOwp9Cgp2b2lkIG1vc3RyYXJfbGlzdGEoRWxlbWVudG8qIGFnZW5kYSl7CiAgICBFbGVtZW50byAqcDsKICAgIGZvcihwID0gYWdlbmRhOyBwICE9IE5VTEw7IHAgPSBwIC0+IHByb3gpewogICAgICAgIHByaW50ZigiJXMgLT4gIiwgcC0+aW5mby5ub21lKTsKICAgIH0KCiAgICBwcmludG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