You need to enable JavaScript to run this app. C语言按字母序存储7字符单词文件时链表全节点输出同一末尾单词问题
优惠活动
大模型
产品
解决方案
定价
更多

C语言按字母序存储7字符单词文件时链表全节点输出同一末尾单词问题

代码故障定位与修复

核心故障根因

  • 浅拷贝导致的存储失效:main函数中定义的char word[8]是栈上的固定复用缓冲区,每次循环读入新单词都会覆盖这块内存的内容。insert函数直接把缓冲区的地址赋值给每个节点的data指针,没有为每个节点独立分配字符串存储空间——所有节点的data都指向同一块栈内存,最终这块内存留存的是最后一次读到的单词,直接表现为链表所有节点输出同一个最后单词。
  • 结构体成员笔误:printList函数遍历链表时引用了不存在的rightPtr成员,正确的后继指针名是nextPtr,这处笔误会直接导致编译失败或运行时内存越界。
  • 字符串比较逻辑错误:排序时写的value > currentPtr->data是直接比较两个字符串的首地址,根本没有按字典序比对字符串内容,完全达不到字母排序的效果,需要替换为标准库的strcmp函数。
  • 冗余且残缺的文件读取逻辑:main中单独写了无参数的残句fseek(),且逐段跳转读文件的方式完全多余,直接顺序读取文件即可,多余的跳转操作很容易出现文件偏移量计算错误。
  • 没有处理fgets读入的尾部换行符:会导致每个单词后面带着多余的换行,输出格式错乱。

可直接运行的修复版代码

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

struct dictionary {
    char *data;
    struct dictionary *nextPtr;
};

typedef struct dictionary Dictionary;
typedef Dictionary* DictionaryPtr;

// 函数声明
void printList(DictionaryPtr currentPtr);
void insert(DictionaryPtr *sPtr, char *value);
int isEmpty(DictionaryPtr sPtr);
void freeList(DictionaryPtr currentPtr);

void insert(DictionaryPtr *sPtr, char *value) {
    // 为每个节点单独分配独立的字符串存储空间
    char *wordCopy = malloc(strlen(value) + 1);
    if (wordCopy == NULL) {
        printf("%s not inserted. No memory available.\n", value);
        return;
    }
    strcpy(wordCopy, value);

    DictionaryPtr newPtr = malloc(sizeof(Dictionary));
    if (newPtr == NULL) {
        printf("%s not inserted. No memory available.\n", value);
        free(wordCopy);
        return;
    }
    newPtr->data = wordCopy;
    newPtr->nextPtr = NULL;

    DictionaryPtr previousPtr = NULL;
    DictionaryPtr currentPtr = *sPtr;

    // 用strcmp实现字典序排序
    while (currentPtr != NULL && strcmp(value, currentPtr->data) > 0) {
        previousPtr = currentPtr;
        currentPtr = currentPtr->nextPtr;
    }

    if (previousPtr == NULL) {
        newPtr->nextPtr = *sPtr;
        *sPtr = newPtr;
    } else {
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    }
}

int isEmpty(DictionaryPtr sPtr) {
    return sPtr == NULL;
}

void printList(DictionaryPtr currentPtr) {
    if (isEmpty(currentPtr)) {
        printf("List is empty.\n");
        return;
    }
    printf("The list is:\n");
    // 修正笔误,使用nextPtr遍历链表
    while (currentPtr != NULL) {
        printf("%s --> ", currentPtr->data);
        currentPtr = currentPtr->nextPtr;
    }
    printf("NULL\n");
}

// 遍历释放所有链表内存
void freeList(DictionaryPtr currentPtr) {
    DictionaryPtr tempPtr;
    while (currentPtr != NULL) {
        tempPtr = currentPtr;
        currentPtr = currentPtr->nextPtr;
        free(tempPtr->data);
        free(tempPtr);
    }
}

int main(void) {
    FILE *fp = fopen("P4WORDS.TXT", "r");
    if (fp == NULL) {
        printf("Dictionary file not included in file structure\n");
        exit(EXIT_FAILURE);
    }

    DictionaryPtr startPtr = NULL;
    char word[16];

    // 顺序读取文件,移除冗余的fseek跳转
    while (fgets(word, sizeof(word), fp) != NULL) {
        // 去掉fgets读到的尾部换行符
        word[strcspn(word, "\n")] = '\0';
        // 跳过空行
        if (strlen(word) == 0) continue;
        insert(&startPtr, word);
    }

    fclose(fp);
    printList(startPtr);
    freeList(startPtr);
    return 0;
}

修复核心逻辑说明

  • 存储环节做深拷贝:在insert函数中为每个节点调用malloc分配独立的字符串内存,通过strcpy把读到的单词拷贝过去,每个节点的字符串存储完全独立,不会被后续的读操作覆盖。
  • 排序环节用strcmp按字典序比对单词,保证链表插入时按字母升序排列。
  • 修正链表遍历的成员名笔误,移除冗余的文件跳转逻辑,直接顺序读文件,同时自动清理读到的换行符。
  • 补充内存释放函数,遍历链表时依次释放节点本身和节点存储的字符串内存,避免内存泄漏。

内容的提问来源于stack exchange,提问作者Kevin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.16 03:15:02