c - 如何比较 C 链表中的单词是否相同

标签 c

代码已给出。我需要计算不相同的单词数量。为了做到这一点,我需要使用 stcrmp 来比较它们。通过查看下面的代码,我需要如何构造 while 或 if 语句来使用双链表比较文件中的单词?我想这个条件应该在主要打印它。我的条件不行。另外,您能否提供一些建议,在哪里以及如何按长度对单词进行排序? 为了理解代码一些解释: 该程序保存一个双向链表,它将读取作为命令行参数输入的文件,从文件中读取每一行,标记行中的每个单词,并且对于每个单词,根据其长度将其放入单词长度结构中,然后会将其放入取决于单词字符串的 word_count 结构中,并计算每个单词在文件中出现的次数。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DELIM " ,.+-=!?:;\t"
#define MAXLINE 25000

typedef struct word_count {
    char *word;
    int count;
    struct word_count *next;
    struct word_count *prev;
} WORD;

typedef struct word_length_count {
    int length;
    int count;
    WORD *words;
    struct word_length_count *next;
    struct word_length_count *prev;
} WLENGTH;

int splitIntoWords(char line[]);
void processLength(char *word);
void processWord(char *word, WORD *wordCount);
void printWordLength();
WLENGTH *createWordLength(char *word);
WORD *createWordCount(char *word);

WLENGTH *wordLength = NULL;

int main(unsigned int argc, unsigned char *argv[]) {
    FILE *fpin;
    char line[MAXLINE];
    int totalWordCount = 0;

    if ((fpin = fopen(argv[1], "r")) == NULL) {
        printf("Can't open input file.\n");
        exit(-1);
    }

    printf("This is the words all tokenized from the input!\n");
    while (fgets(line, MAXLINE, fpin) != NULL) {
        line[strcspn(line, "\n")] = '\0';
        if (line[0] == '\0')
        continue;
        totalWordCount += splitIntoWords(line);
    }
    printf("Total number of words is: %d\n", totalWordCount);
    printWordLength();
    printf("\nFINISHED!");
}

int splitIntoWords(char line[]) {
    char *word;
    int count=0;
    word = strtok(line, DELIM);
    for (;word != NULL;) {
        count++;
        printf("%s\n", word);
        processLength(word);
        word = strtok(NULL, DELIM);
    }
    return count;
}

void processLength(char *word)
{
    WLENGTH *wLCounter = NULL;
    WLENGTH *wLLast = NULL;

    if (wordLength == NULL) {
        wordLength = createWordLength(word);
        return;
    }
    wLCounter = wordLength;
    while (wLCounter != NULL) {
        if (strlen(word) == wLCounter->length) {
            ++wLCounter->count;
            processWord(word, wLCounter->words);
            return;
        }
        wLLast = wLCounter;
        wLCounter = wLCounter->next;
    }
    wLLast->next = createWordLength(word);
}

void processWord(char *word, WORD *wordCount) {
    WORD *wCounter = NULL;
    WORD *wLast = NULL;

    if (wordCount == NULL) {
        wordCount = createWordCount(word);
        return;
    }
    wCounter = wordCount;
    while (wCounter != NULL) {
        if (strcmp(word, wCounter->word) == 0) {
            ++wCounter->count;
            return;
        }
        wLast = wCounter;
        wCounter = wCounter->next;
    }
    wLast->next = createWordCount(word);
}

WLENGTH *createWordLength(char *word) {
    WLENGTH *wLCounter = NULL;
    wLCounter = (WLENGTH*)malloc(sizeof(WLENGTH));
    wLCounter->words = createWordCount(word);
    wLCounter->count = 1;
    wLCounter->length = strlen(word);
    wLCounter->next = NULL;
    return wLCounter;
}

WORD *createWordCount(char *word) {
    WORD *wCount = NULL;
    wCount = (WORD*)malloc(sizeof(WORD));
    wCount->word = (char*)malloc(strlen(word+1));
    strcpy(wCount->word, word);
    wCount->count = 1;
    wCount->next = NULL;
    return wCount;
}

void printWordLength() {
    WLENGTH *temp = wordLength;
    WORD *tempWORD = wordLength->words;
    while (temp != NULL) {
        WORD *tempWORD = wordLength->words;
        tempWORD = temp->words;
        printf("\nFor Word Length: %d : There are: %d occurances!\n", temp->length, temp->count);
        while (tempWORD != NULL) {
            printf("\t%s\toccurs:%d\n", tempWORD->word, tempWORD->count);
            tempWORD = tempWORD->next;
        }
    }
}

最佳答案

您在 printWordLength() 最外层 while 循环的底部缺少了这个:

temp  = temp->next;

这就是为什么它会进入无限循环(您没有告诉我们)。

现在,要计算不同的单词,您只需计算每个 WORDLENGTH* 中的每个 WORD*,您可以在 中打印它们时执行此操作printWordLength():

void printWordLength()
{
    WLENGTH * temp = wordLength;
    WORD * tempWORD = wordLength->words;
    unsigned int unique_words = 0;
    while(temp != NULL)
    {
        WORD * tempWORD = wordLength->words;
        tempWORD = temp->words;
        printf("\nFor Word Length: %d : There are: %d occurences!\n",
                temp->length, temp->count);
        while(tempWORD != NULL)
        {
            printf("\t%s\toccurs:%d\n", tempWORD->word, tempWORD->count);
            unique_words++;
            tempWORD = tempWORD->next;
        }
        temp  = temp->next;
    }
    printf("\nThere are %u unique words\n", unique_words);
}

关于c - 如何比较 C 链表中的单词是否相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41990535/

相关文章:

cmake 不显示显示消息

无法发现我的 GLSL/OpenGL 代码的问题

c - 如何摆脱 doxygen 配置文件中的文件引用?

C - 函数内的数组初始化

c - 为什么即使没有基本情况,递归也能起作用?

c - union 内的无名 union

c - 为什么在尝试使用指针访问数组元素时不使用间接运算符?

c - 尝试使用C编程中的结构和函数将单数名词改为复数

c++ - 如何针对大量整数优化 C++/C 代码

c - 我在我的程序中找不到内存泄漏