常用词检查循环不起作用

时间:2017-10-17 16:47:53

标签: c linked-list

我想创建一个读取两个文件的程序,并给出常用单词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 adocument ,这是第一个文件的最后两个字,甚至不常见。我认为 strcmp功能有一些问题。

之前我问了一个类似的问题,但它们并不相同。

以下是我打印match2链表的方法。

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;

1 个答案:

答案 0 :(得分: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行应该strdup new_data

如果仔细观察,insert将使用destination1调用,if(new_node == NULL){ *new_node = ptr1; return; } 是一个固定缓冲区。因此,如果每次创建新节点时都不复制其内容,则每个节点都将指向同一个缓冲区,该缓冲区将包含最后两个单词。

同时

那部分

new_node

可能是死代码,也就是说,if永远不会为NULL,因为您的列表头是预先初始化的(我们永远不会确定您是否发布了完整的代码)。

如果这不是死代码(您可以通过new_node内的printf'进行检查),那么此处存在一个错误,因为当NULL*new_node时,然后header { min-height: 4em; background: /* first the mask */ linear-gradient(to bottom right, transparent 49.5%, white 50.5%) bottom left no-repeat, /* then the background image */ url(http://lorempixel.com/400/300/abstract/1) 0 0; /* finally resize each image, in particular the mask */ background-size: 100% 4em, cover; padding: 1em 2em 4em; color: white; } body { margin: 0; } div { padding: 1em; }取消引用NULL,这应该触发SIGSEGV。