Reputation: 33
I made a linked list like this:
typedef struct {
char *id;
char *nombre;
char *region;
char *partido;
int edad;
struct votante *siguiente;
} votante;
And I have a function that creates new nodes with some data read from a text file. The thing is, I have to search for people with the same ID but different "partido" (party, like in politics). But I'm having trouble on displaying that info while I'm moving in the list. I have a code that moves along the whole list and it compares some X position with the other position on the right of X. The problem is, the info is repeated, because I'm searching for every possible combination that checks my two conditions. I think I should delete a node after I check it to avoid this, and keep every deleted node in another list with only deleted people, but I don't know how to implement this. Here is my function:
votante *consultarDobleRegistro(votante *lista){
votante *aux = lista;
votante *aux2 = aux->siguiente;
votante *eliminar;
votante *fraudes = crearVotante();
int encontrar = 0;
int vueltas = 0;
while(aux->siguiente != NULL){
//si existe el mismo ID en diferentes partidos
if(strcmp(aux->id, aux2->id) == 0 && strcmp(aux->partido, aux2->partido) != 0){
// agrego a "fraudes" el resultado que está después del parámetro a comparar
encontrar++;
if(encontrar==1){
printf("encontro aux: %s - %s\n", aux->id, aux->partido);
}
printf("encontro aux2: %s - %s\n", aux2->id, aux2->partido);
fraudes = agregarNodo(fraudes, aux2->id, aux2->nombre, aux2->region, aux2->partido, aux2->edad);
if(aux2->siguiente == NULL){
aux = aux->siguiente;
aux2 = aux->siguiente;
} else {
aux2 = aux2->siguiente;
}
} else {
if(aux2->siguiente == NULL){
aux = aux->siguiente;
encontrar = 0;
vueltas++;
aux2 = aux->siguiente;
} else {
aux2 = aux2->siguiente;
}
}
}
printf("vueltas: %d\n", vueltas);
return fraudes;
}
I need to show the nodes with the same "ID" but different "partido" (or get them into a new list so I can use my show() function to show them later).
Upvotes: 0
Views: 164
Reputation: 510
The code you provide is not showing the main problem.
According to your description, what you should focus is how you travel and compare the whole list.
I'm not very good at algorithm, so the solution may not very at efficiency, but wish to provide some basic idea:
The main purpose is to categorize id, having new linked list struct like this:
struct votante_same_id {
struct votante *id_group;
struct votante_same_id *next;
};
struct votante_id_group {
char *id;
struct votante_same_id head;
struct votante_id_group *next;
};
Then you travel the whole votante list, compare each id with votante_id_group->id,
when a new id is found, add it to votante_id_group; else add that votante to existing votante_same_id list.
When trave of votante end, now you travel votante_id_group, and then to categorize partido, similiar to above.
Finally votante node in different list should be what you need.
Upvotes: 1