I'm trying to copy unique words from a .txt file to a linked list. I need to copy every single unique word once without any repetitions. I wrote a program, but I have a problem with the function which checks if the word already exists in the list. Below is the code snippet which reads words from the file and adds the word to the linked list if it's not already added.
FILE*fd;
fd=fopen("c:\\texto.txt","r");
while(!feof(fd))
{
fscanf(fd,"%s",aux);
if(check(&lista,aux)==1){
add(&lista,aux);}
}
My idea of the check
function is: check if the actual word I got from the file is the same as the one I already have in the current node in the linked list. If it is not present in the current node, go to the next node. If the function finds that the word is already in the list, return 0. In case the function reaches the end of the linked list, return 1, so that the main can add the word to the linked list.
int check (t_nodo*lista, char*aux)
{
if((*lista)!=NULL&&strcmp(((*lista)->word,aux))!=0)
{
check(&(*lista)->next,aux);
}
if((((*lista)->word,aux))==0)
{
return 0;
}
if((*lista)==NULL)
{
return 1;
}
}//el tano se la come a mordiscones
Actually, the program is copying only the first word to the list. Then it sets the word of all the other nodes same as the first word. I've already checked the "add" function and it's ok.