I want to make a program that reads two file and gives me the common word as 2-gram. I wrote the below code.
here is the node
struct node {
char *string;
struct node *next;
};
here is the check loop
struct node *sw1, *sw2, *sw1_head;
//first 1 and first2 is the head of linked lists that holds text's each word seperatly. i created before.
first1 = first1_head; // _ head is the same as first1 and first2
first2 = first2_head;
//sw1 and sw2 are the pointers that holds always second words.
sw2 = first2->next;
sw1 = first1->next;
sw1_head = sw1;
//these chars are used to concat two words
char destination1[50];
char destination2[50];
while(sw2 != NULL){
strcpy(destination2,first2->string);
strcat(destination2,sw2->string);
while(sw1 != NULL){
strcpy(destination1,first1->string);
strcat(destination1,sw1->string);
// printf("%s\n", destination1);
if(strcmp(destination2, destination1) == 0) {
insert(&matched2, destination1);//matched holds common words
}
sw1 = sw1->next;
first1 = first1->next;
}
sw1 = sw1_head;//sets both sw1 and first1 to their past positions.
first1 = first1_head;
sw2 = sw2->next;
first2 = first2->next;
}
When i tried to print the matched2 linkedlist. It gives me 21 adocument which is the last two words of first file which is not even common. I think there is something wrong in strcmp function .
I asked a similar question before but they are not same.
Here is how i print matched2 linked-list.
while(matched2 != NULL){
printf("%s\n", matched2->string);
matched2 = matched2->next;
}
here is the insert
method
void insert(struct node **new_node, char* new_data){
/* allocate node */
struct node *ptr1 =(struct node*) malloc(sizeof(struct node));
/* put in the data */
ptr1->string = new_data;
ptr1->next = NULL;
if(new_node == NULL){
*new_node = ptr1; return;
}
ptr1->next = *new_node;
*new_node = ptr1;
Change your insert
function to:
void insert(struct node **new_node, char* new_data){
/* allocate node */
struct node *ptr1 =(struct node*) malloc(sizeof(struct node));
/* put in the data */
ptr1->string = strdup(new_data);
ptr1->next = NULL;
if(new_node == NULL){
*new_node = ptr1; return;
}
ptr1->next = *new_node;
*new_node = ptr1;
The only change is that the line ptr1->string = new_data
should strdup
the new_data
.
If you look closely, insert
is invoked with destination1
, which is a fixed buffer. So if you do not copy its content each time you create a new node, every node will end pointing to the very same buffer, which will contain the last two words.
Also
That part
if(new_node == NULL){
*new_node = ptr1; return;
}
is probably dead code, that is, new_node
is never NULL perhaps because your list head is pre-initialized (we will never know for sure if you do not post your complete code).
If this is not dead code (you can check it by printf'ing right inside the if
), then there is a bug lurking here, since when new_node
is NULL
, then *new_node
dereferences NULL, which should trigger SIGSEGV.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.