感谢 @guaiguaitinghua 的归并链表排序
思路:构建链式散列表,再组合成按词频逆序、字典序顺序排列的链表
主要的几个问题:
- 数据结构的构建(链式散列表)
- 读入单词并忽略大小写(统一转换成小写)
- 链表排序,如果词频一样比较字典序
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PERCENT 10
#define MAXTABLESIZE 200
#define TABLESIZE 100
#define WORDLEN 15
typedef char *ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
int Count;
PtrToLNode Next;
};
typedef PtrToLNode Position;
struct HNode {
PtrToLNode *Head;
int Size;
};
typedef struct HNode *HashTable;
int NextPrime(int N)
{
int i, P;
P = (N % 2) ? N + 2 : N + 1;
while (P <= MAXTABLESIZE) {
for (i = (int)sqrt(P); i > 2; i--) {
if (!P % i) {
break;
}
}
if (i == 2) {
break;
} else {
P += 2;
}
}
return P;
}
int Hash(const char *Key, int TableSize)
{
unsigned int H;
H = 0;
while (*Key) {
H = (H << 5) + *Key++;
}
return H % TableSize;
}
char toLowerCase(char c) {
return c + ('a' - 'A');
}
bool isWordCharacter(char c)
{
return (c >= 'a' && c <= 'z') || (c >=<