Files
leetcode/we/23-2.h
2025-09-15 21:12:04 +08:00

60 lines
1.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct ListNode {
char *val;
int count;
struct ListNode *next;
};
struct ListNode *initNode(char *val) {
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
// 返回值带头节点
struct ListNode *process(const char *fileName){
struct ListNode *head = initNode("");
FILE *fp = fopen(fileName, "r");
if(fp == NULL) {
printf("open file failed\n");
return head;
}
char buffer[1024];
char *words[1000];
int wordCount = 0;
while (fscanf(fp, "%s", buffer) != EOF && wordCount < 1000) {
words[wordCount] = strdup(buffer); // 使用 strdup 复制字符串
wordCount++;
}
int tag = 0; // 0 表示还没有处理1 表示已经处理
for (int i = 0; i < wordCount; i++) {
struct ListNode *p = head;
tag = 0;
while(p->next != NULL) {
if(strcmp(p->next->val, words[i]) == 0){
p->next->count++;
tag = 1;
break;
}
p = p->next;
}
if(tag == 0) {
struct ListNode *node = initNode(words[i]);
node->count = 1;
p->next = node;
}
}
fclose(fp);
return head;
}