[Q] Is there any one good at linked lists - C 10-20-2017, 06:33 PM
#1
This is a beginning of my homework but I don't know what is the problem in my code.
I need to store every word separately in a linked list.
Splitting process is just working fine, but problem in the add() function I think.
In the main func. I send every word to add() function to add in linked list's end. I assign new word into current->data and It seems working fine when I output it inside the add() function, but the problem is also head node which is my root node is also changing, it shouldn't be changed.
Help me please
here is the sample Input1.txt
As seen on output below, all my current->data and head->data are same in every loop. But head->data have to stay as first word('Term' in this case)
![[Image: ZOZWrg.png]](https://i.hizliresim.com/ZOZWrg.png)
My codes: http://codepad.org/aGtLDUja
I need to store every word separately in a linked list.
Splitting process is just working fine, but problem in the add() function I think.
In the main func. I send every word to add() function to add in linked list's end. I assign new word into current->data and It seems working fine when I output it inside the add() function, but the problem is also head node which is my root node is also changing, it shouldn't be changed.
Help me please
here is the sample Input1.txt
sample text Wrote:Term Frequency is a feature representation technique. Term frequency is often used in
text mining field. Term Frequency measures how frequently a term occurs in a
document.
As seen on output below, all my current->data and head->data are same in every loop. But head->data have to stay as first word('Term' in this case)
![[Image: ZOZWrg.png]](https://i.hizliresim.com/ZOZWrg.png)
My codes: http://codepad.org/aGtLDUja
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct liste
{
char * data;
struct liste * next;
};
typedef struct liste Node;
Node *current,*head;
void add(char *chr)
{
current->data = chr;
Node *tmp = (Node*)malloc(sizeof(Node));
tmp->next = NULL;
current->next = tmp;
printf("word added: %s \t\t head: %s\n",current->data,head->data);
current = current->next;
}
int main() {
current = (Node *)malloc(sizeof(Node));
current->next = NULL;
head = current;
FILE *dosya = fopen("C:/Users/omerf/CLionProjects/untitled/Input1.txt","r");
if (dosya == NULL )
{
printf("dosya acilamadi\n");
exit(1);
}
char c[20];
while (!feof(dosya))
{
fscanf(dosya,"%s",c);
int i=0;
for(i=0 ; i< strlen(c) ; i++)
{
if(!isalpha(c[i])) c[i] = '\0';
}
add(c);
}
fclose(dosya);
return 0;
}
![[Image: xh31A6W.gif]](https://i.imgur.com/xh31A6W.gif)