1
0

23-2.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. struct ListNode {
  5. char *val;
  6. int count;
  7. struct ListNode *next;
  8. };
  9. struct ListNode *initNode(char *val) {
  10. struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
  11. node->val = val;
  12. node->next = NULL;
  13. return node;
  14. }
  15. // 返回值带头节点
  16. struct ListNode *process(const char *fileName){
  17. struct ListNode *head = initNode("");
  18. FILE *fp = fopen(fileName, "r");
  19. if(fp == NULL) {
  20. printf("open file failed\n");
  21. return head;
  22. }
  23. char buffer[1024];
  24. char *words[1000];
  25. int wordCount = 0;
  26. while (fscanf(fp, "%s", buffer) != EOF && wordCount < 1000) {
  27. words[wordCount] = strdup(buffer); // 使用 strdup 复制字符串
  28. wordCount++;
  29. }
  30. int tag = 0; // 0 表示还没有处理,1 表示已经处理
  31. for (int i = 0; i < wordCount; i++) {
  32. struct ListNode *p = head;
  33. tag = 0;
  34. while(p->next != NULL) {
  35. if(strcmp(p->next->val, words[i]) == 0){
  36. p->next->count++;
  37. tag = 1;
  38. break;
  39. }
  40. p = p->next;
  41. }
  42. if(tag == 0) {
  43. struct ListNode *node = initNode(words[i]);
  44. node->count = 1;
  45. p->next = node;
  46. }
  47. }
  48. fclose(fp);
  49. return head;
  50. }
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。