-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path11-散列1 电话聊天狂人.c
166 lines (146 loc) · 3.46 KB
/
11-散列1 电话聊天狂人.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#define KEYLENGTH 11
#define MAXD 5
#define MAXTABLESIZE 1000000
/* ——————HashTable定义开始—————— */
typedef char ElementType[KEYLENGTH + 1];
typedef int Index;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
int Count;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
typedef struct TblNode *HashTable;
struct TblNode {
int TableSize;
List Heads;
};
/* ——————HashTable定义结束—————— */
int NextPrime(int N);
int Hash(int Key, int p);
Position Find(HashTable H, ElementType Key);
HashTable CreateTable(int TableSize);
bool Insert(HashTable H, ElementType Key);
void ScanAndOutput(HashTable H);
void DestoryTable(HashTable H);
int main()
{
int N, i;
ElementType Key;
HashTable H;
scanf("%d", &N);
H = CreateTable(N * 2);
for (i = 0; i < N; ++i) {
scanf("%s", Key); Insert(H, Key);
scanf("%s", Key); Insert(H, Key);
}
ScanAndOutput(H);
DestoryTable(H);
return 0;
}
int NextPrime(int N)
{
int i, 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(int Key, int P)
{
return Key % P;
}
Position Find(HashTable H, ElementType Key)
{
Position P;
Index Pos;
Pos = Hash(atoi(Key + KEYLENGTH - MAXD), H->TableSize);
P = H->Heads[Pos].Next;
while(P && strcmp(P->Data, Key))
P = P->Next;
return P;
}
HashTable CreateTable(int TableSize)
{
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = NextPrime(TableSize);
H->Heads = (List)malloc(H->TableSize * sizeof(struct LNode));
for (i = 0; i < H->TableSize; ++i) {
H->Heads[i].Data[0] = '\0'; H->Heads[i].Next = NULL;
H->Heads[i].Count = 0;
}
return H;
}
bool Insert(HashTable H, ElementType Key)
{
Position P, NewCell;
Index Pos;
P = Find(H, Key);
if (!P) {
NewCell = (Position)malloc(sizeof(struct LNode));
strcpy(NewCell->Data, Key);
NewCell->Count = 1;
Pos = Hash(atoi(Key + KEYLENGTH - MAXD), H->TableSize);
NewCell->Next = H->Heads[Pos].Next;
H->Heads[Pos].Next = NewCell;
return true;
}
else {
++P->Count;
return false;
}
}
void ScanAndOutput(HashTable H)
{
int i, MaxCnt, PCnt;
ElementType MinPhone;
List Ptr;
MinPhone[0] = '\0';
MaxCnt = PCnt = 0;
for (i = 0; i < H->TableSize; ++i) {
Ptr = H->Heads[i].Next;
while (Ptr) {
if (Ptr->Count > MaxCnt) {
MaxCnt = Ptr->Count;
strcpy(MinPhone, Ptr->Data);
PCnt = 1;
}
else if (Ptr->Count == MaxCnt) {
++PCnt;
if (strcmp(MinPhone, Ptr->Data) > 0)
strcpy(MinPhone, Ptr->Data);
}
Ptr = Ptr->Next;
}
}
printf("%s %d", MinPhone, MaxCnt);
if (PCnt > 1) printf(" %d", PCnt);
printf("\n");
}
void DestoryTable(HashTable H)
{
int i;
Position P, Tmp;
for (i = 0; i < H->TableSize; ++i) {
P = H->Heads[i].Next;
while (P) {
Tmp = P->Next;
free(P);
P = Tmp;
}
}
free(H->Heads);
free(H);
}