【问题标题】:Single linked lists in CC中的单链表
【发布时间】:2011-01-19 13:24:20
【问题描述】:

我基本上是在尝试从文本文件创建一个链接列表,并在每次单词不同时添加一个新成员,如果单词相同则增加计数(硬件分配)。我以为我做得对,但无论如何似乎都添加了一个成员。我想知道我在搜索时是否错误地遍历了列表?这是我的代码。有什么想法吗?谢谢!

LIST *CreateList(FILE *fp) 
{
    char input[LINE_LEN];
    LIST *root= NULL;             /* contains root of list             */
    size_t strSize;    
    LIST *newList;          /* used to allocate new list members */
    int same;             /* if string is same */

    while (fscanf(fp, BUFFMT"s", input) != EOF) {

        strSize = strlen(input) + 1;

            if (root == NULL) {
                if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) {
                    printf("Out of memory...");
                    exit(EXIT_FAILURE);
                } 
                if ((newList->str = (char *)malloc(sizeof(strSize))) == NULL) {
                    printf("Not enough memory for %s", input);
                    exit(EXIT_FAILURE);
                }
                memcpy(newList->str, input, strSize);   /*copy string    */
                newList->count = START_COUNT;
                newList->next = NULL;
                root = newList;
            }
            /* if not root node, add node, or increment count */
            else {
                same = ListSame(newList, input);
                if (same == 1) {
                    root->count++;
                }
                else {
                    if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) {
                        printf("Out of memory...");
                        exit(EXIT_FAILURE);
                    } 
                    if ((newList->str = (char *)malloc(sizeof(strSize))) == NULL) {
                        printf("Not enough memory for %s", input);
                        exit(EXIT_FAILURE);
                    }
                    memcpy(newList->str, input, strSize);   /*copy string    */
                    newList->count = START_COUNT;
                    newList->next = root->next;
                    root->next = newList;
                }
            }
    }
        return root;
}

int ListSame(LIST *head, char *input) 
{
    LIST *start = head;
    for (; start != NULL; start = start->next) {
        if (strcmp(head->str, input) == 0) {
            return 1;   
        }
    }
    return 0;
}

【问题讨论】:

标签: c linked-list


【解决方案1】:

您使用 newList 调用 ListSame,它始终只有最后创建的节点。看起来您想将 root 传递给 ListSame。

此外,在 ListSame 中,它始终检查 head->str,而不是 start->str,这是您的循环变量。

此外,如果 ListSame 返回 true (1),则增加 root->count;我不确定您是否要增加根节点的计数(计算总共有多少重复项)或具有重复项的节点的计数(计算每个单词出现的次数)。如果是后者,ListSame 要么需要返回哪个节点是重复的,要么需要增加计数本身。

此外,您构建列表的方式有点令人困惑。当你说:

newList->next = root->next;
root->next = newList;

root->next 在此之前将是 NULL,或者是最后创建的节点。因此,当您在此处进行插入时,您总是将其作为列表中的第二项插入。这可能不是你想要的。如果要追加,则应跟踪尾部和头部;如果你想预先设置,只需设置newList->next = root; root = newList;

【讨论】:

    【解决方案2】:
    LIST *start = head;
    for (; start != NULL; start = start->next) {
        if (strcmp(head->str, input) == 0) {
            return 1;   
        }
    }
    

    你应该使用

    strcmp(start->str
    

    【讨论】:

      猜你喜欢
      • 2014-04-04
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      相关资源
      最近更新 更多