c - 如何在 C 中按字母顺序对文件的行进行排序?

标签 c file sorting linked-list

<分区>

好的,我现在所拥有的是检查字数。但是我无法尝试按字母顺序对单词进行排序。

我宁愿那样做,也不愿只计算它们的数量。

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

typedef struct node *node_ptr; 

typedef struct node {
    int count;
    char *word;
    node_ptr next;
} node_t;  

char *words[] = { "hello", "goodbye", "sometimes", "others", "hello", "others", NULL }; 

node_ptr new_node() {
    node_ptr aNode;
    
    aNode = (node_ptr)(malloc(sizeof(node_t)));
    if (aNode) {
        aNode->next = (node_ptr)NULL;
        aNode->word = (char *)NULL;
        aNode->count = 0;
    }
    return aNode;
}

node_ptr add_word(char *word, node_ptr theList) {
    node_ptr currPtr, lastPtr, newPtr;
    int result;
    int found = 0;
    
    currPtr = theList;
    lastPtr = NULL;
    printf("Checking word:%s\n", word);
    
    if (!currPtr) {
        newPtr = new_node();
        if (!newPtr) {
            fprintf(stderr, "Fatal Error. Memory alloc error\n");
            exit(1);
        }
        newPtr->word = word;
        newPtr->next = currPtr;
        newPtr->count = 1;
        found = 1;
        theList = newPtr;
    }
    while (currPtr && !found) {
        result = strcmp(currPtr->word, word); 
        if (result == 0) {
            currPtr->count += 1;
            found = 1;
        } else
        if (result>0) {
            newPtr = new_node();
            if (!newPtr) {
                fprintf(stderr, "Fatal Error. Memory alloc error\n");
                exit(1);
            }
            newPtr->word = word;
            newPtr->next = currPtr;
            newPtr->count = 1;

            if (lastPtr) {
                lastPtr->next = newPtr;
            } else {
                theList = newPtr;
            }
            found = 1;
        } else {
            lastPtr = currPtr;
            currPtr = currPtr->next;
        }
    }

    if ((!found) && lastPtr) {
        newPtr = new_node();
        if (!newPtr) {
            fprintf(stderr, "Fatal Error. Memory alloc error\n");
            exit(1);
        }
        newPtr->word = word;
        newPtr->next = (node_ptr)NULL;
        newPtr->count = 1;
        lastPtr->next = newPtr;
        found = 1;
    }
    return theList; 
}

void printList(node_ptr theList) {
    node_ptr currPtr = theList;
    
    while (currPtr) {
        printf("word: %s\n", currPtr->word);
        printf("count: %d\n", currPtr->count);
        printf("---\n");
        currPtr = currPtr->next; 
    }
}

int main() {
    char **w = words;
    node_ptr theList = (node_ptr)NULL;
    
    printf("Start\n");
    while (*w) {
        theList = add_word(*w, theList);
        w++;
    }
    
    printList(theList);
    printf("OK!\n");
    return 0;
}

我也不想从单词数组中读取,而是从文件中读取。

FILE *fp;
fp = fopen("some.txt", "w");

如何使用我创建的结构从文件中读取数据,然后对它们进行排序?

最佳答案

您可以使用 fscanf() 从文件中读取单词:

int main(int argc, char *argv[]) {
    node_t *theList = NULL;
    for (int i = 1; i < argc; i++) {
        FILE *fp = fopen(argv[i], "r");
        if (fp != NULL) {
            char word[100];
            while (fscanf(fp, "%99s", word) == 1) {
                theList = add_word(word, theList);
            }
            fclose(fp);
        }
    }
    printList(theList);
    printf("OK!\n");
    return 0;
}

关于c - 如何在 C 中按字母顺序对文件的行进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960546/

相关文章:

c# - 作为约束 Delaunay 三角剖分的结果识别出多边形三角剖分

c - 用C中的for循环制作矩阵

c - 如何确定 UDP 数据包源自的远程端点?

java - 对通用数据结构数组进行排序

sorting - 一个用于对通用集合进行排序的排序器函数

python - 在 Python 中根据属性对对象列表进行排序的更快方法

c - 如何在 c 中使用 argc、argv[] 的同时提示用户输入?

java - 读取纯文本文件

ios - Xcode Reverted to old version,如何恢复?

r - 如何以制表符分隔的方式写入文件?