2017-10-17 55 views
0

我想制作一个程序来读取两个文件并将常用单词作为2-gram。我写了下面的代码。常见单词检查循环不起作用

这里是节点

struct node { 
    char *string; 
    struct node *next; 
}; 

这里是检查循环

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; 
    } 

,当我试图打印matched2链表。它给我21 文档这是第一个文件的最后两个单词甚至不常见。我认为strcmp函数有问题。

我之前问过类似的问题,但他们不一样。

这里是我如何打印matched2链表。

while(matched2 != NULL){ 
    printf("%s\n", matched2->string); 
    matched2 = matched2->next; 
} 

这里是insert方法

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; 
+0

你真的必须初始化你的字符串缓冲区,否则他们会充满垃圾数据。 – tadman

+0

@tadman我指定他们什么值? –

+0

根据您的偏好,您总是可以用'memset'或'bzero'将它归零。初始化为零字节也是一种选择。 C字符串是NULL终止的,因此您需要在使用任何标准字符串函数之前终止它们。 – tadman

回答

1

更改为insert功能:

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; 

唯一的变化是,行ptr1->string = new_data应该strdupnew_data

如果仔细观察,insertdestination1一起调用,这是一个固定的缓冲区。因此,如果每次创建新节点时都不复制其内容,则每个节点都将结束指向相同的缓冲区,该缓冲区将包含最后两个单词。

此外

那部分

if(new_node == NULL){ 
*new_node = ptr1; return; 
} 

可能是死代码,也就是说,new_node永远不能为null也许是因为你的表头是预先初始化(我们永远不会知道,如果你不要发布你的完整代码)。

如果不是死代码(您可以通过printf'ing权if内部检查),然后有一个错误潜伏在这里,因为当new_nodeNULL,然后*new_node解引用NULL,它应该引发SIGSEGV。

+0

谢谢你真的让我看到了问题。 –

+0

我忘了问。我的编译器不接受'ptr1-> string = strcpy(new_data);' –

+0

对不起,我的意思是'strdup'。 – rslemos