c - 如何在链接列表中添加单词,同时检查和更新c中单词的频率

标签 c list struct

我正在尝试完成一项作业,但遇到了一些问题。我必须编写一个程序,使用 struct 创建一个链接列表,我必须在其中添加单词。如果该单词已经在链接列表中,那么我只需更新频率即可。

我已经有了这个:

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

typedef struct words Words;
struct words{
    char *word;
    int freq;
    Words *next;
};

/*
Inserts a copy of newWord into the list, in lexicographical order. If newWord is already
in the list, increment the freq member of the node. The function returns a pointer to
the list.
*/
Words *addWord(Words *headPtr, char* newWord){
Words *current = headPtr;
if(headPtr == NULL)
{
    current->word = newWord;
    current->freq = 1;
}
else
{
    while(current != NULL)
        if(strcmp(headPtr->word, newWord))
        {
            current->freq++;
            return headPtr;
        }
        else
        {
            current->word = newWord;
            current->freq = 1;
        }
}
return headPtr;
}

//prints the words in the list, along with the frequency of each word
void printWords(Words *headPtr){

    while(headPtr != NULL)
    {
        printf("%s: %d", headPtr->word, headPtr->freq);
        headPtr = headPtr->next;
    }
}

//frees the entire list. Note: Words **headPtr since the headPtr NULL upon return
void deleteList(Words **headPtr){
    Words *current = *headPtr;
    Words *next;
    while(current != NULL)
    {
        next = current->next;
        free(current);
        current = next;
    }
    *headPtr = NULL;
}

int main(){
    char word[20];
    Words *list = NULL;
    scanf("%s", word);
    while(!feof(stdin)){
        list = addWord(list, word);
        scanf("%s", word);
    }
    printWords(list);
    deleteList(&list);
}

最佳答案

您的代码存在一些问题。查看嵌入到代码中的注释:

Words *addWord(Words *headPtr, char* newWord){
    Words *current = (Words*) malloc(sizeof(Words));  // Don't malloc here.
                                                      // You don't know yet
                                                      // whether you need
                                                      // a new node or you
                                                      // you just need to
                                                      // update freq

    if(current == NULL)                   // If current is NULL you have
                                          // serious problems, i.e. you
                                          // are out of memory.
                                          // Did you really intended to do:
                                          //   if (headPtr == NULL)

    {
        current->word = newWord;
        *current->next = (*headPtr);
        (*headPtr) = *current;            // I'm not sure what you try here
                                          // but it seems strange
    }
    else
    {
        while(current != NULL)
            if(strcmp(headPtr->word, newWord))  // This is not the way to compare
                                                // strings. Two strings compare 
                                                // when "strcmp" returns 0.
                                                //
                                                // Further you don't want to
                                                // use headPtr here.

            {
                current->freq++;  // Use of uninitialized value
                return;           // Missing argument to return
            }
            else
            {
                current->word = newWord;       // Use of uninitialized value
                *current->next = (*headPtr);   // Use of uninitialized value
                (*headPtr) = *current;
            }
    }

   // Missing return

}

这里是一些开始的代码:

#define WORD_SIZE 20

struct words{
    char word[WORD_SIZE];   // Use a char array
    int freq;
    Words *next;
};

Words *addWord(Words *headPtr, char* newWord)
{
    Words *current = headPtr;  // Make a copy of headPtr
    Words* new;

    if ((current == NULL) || (strcmp(current->word, newWord) > 0))
    {
         // Insert in front of list
        new = malloc(sizeof(Words)); // Allocate memory
        if (new == NULL)
        {
            // oh, dear - out of memory - print an error message and exit
            exit(1);
        }

        strncpy(new->word, newWord, WORD_SIZE); // Make sure not to overflow 
                                                    // the buffer, so use strncpy
        (new->word)[WORD_SIZE-1] = '\0';   // Make sure to zero terminate
        new->freq = 1;
        new->next = headPtr;

        return new;
    }

    while(1)
    {
        int cmp = strcmp(current->word, newWord);
        if(cmp == 0)
        {
             current->freq++;
             return headPtr;
        }

        if(cmp < 0)
        {
            if ((current->next == NULL) || (strcmp(current->next->word, newWord) > 0))
            {
                // Add code to insert node after current

                return headPtr;
            }
        }
        else
        {
             // This should never happen...
             printf("BAD CODE 1\n");
             exit(1);
        }

        current = current->next;
    }
}

关于c - 如何在链接列表中添加单词,同时检查和更新c中单词的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918217/

相关文章:

ios - Objective-C语法问题

c - ap_log_error 没有登录我的 apache 模块

java将列表元素显示为字符串

三元语句中的 C 逗号

C 编程为什么在值之前打印垃圾?

python - 如何将 .txt 文件转换为列表 python?

python - 使用 .find 在列表中搜索字符串

c - 请求成员不在结构中

c - 结构体内部的结构体?

c# - 通用约束,其中 T : struct and where T : class