#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define KEYLENGTH 11
#define MAXTABLESIZE 1000
#define MinTableSize 5
#define MAXD 5
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;
};
int Hash(int Key,int P);
int NextPrime(int N);
int sqrtBinary(int p);
HashTable CreateTable(int TableSize);
Position Find(HashTable H,ElementType Key);
bool Insert(HashTable H,ElementType Key);
void ScanfAndOutput(HashTable H);
void ScanfAndOutput(HashTable H){
int i,MaxCount = 0;
int PCount = 0;
ElementType MinPhone;
List Ptr;
MinPhone[0] = '\0';
for(i = 0;i < H->TableSize;i++){
Ptr = H->Heads[i].Next;
while(Ptr){
if(Ptr->Count > MaxCount){
MaxCount = Ptr->Count;
strcpy(MinPhone,Ptr->Data);
PCount = 1;
}else if(Ptr->Count == MaxCount){
PCount++;
if(strcmp(MinPhone,Ptr->Data) > 0){
strcpy(MinPhone,Ptr->Data);
}
}
Ptr = Ptr->Next;
}
}
printf("%s %d",MinPhone,MaxCount);
if(PCount > 1){
printf(" %d",PCount);
}
printf("\n");
}
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 true;
}
}
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;
if(TableSize < MinTableSize){
printf("散列表太小");
return NULL;
}
H = (HashTable)malloc(sizeof(struct TblNode));
if(H == NULL){
printf("空间溢出");
}
H->TableSize = TableSize;
H->Heads = (List)malloc(sizeof(struct LNode)*H->TableSize);
if(H->Heads == NULL){
printf("空间溢出");
}
for(int i = 0;i < H->TableSize;i++){
H->Heads[i].Data[0] = '\0';
H->Heads[i].Next = NULL;
H->Heads[i].Count = 0;
}
return H;
}
int Hash(int Key,int P){
return Key%P;
}
int main () {
int N,i,HashTableSize;
ElementType Key;
HashTable H;
H = CreateTable(5);
for(i = 0;i < 4;i++){
printf("输入第%d对的第一个号码:\n",i);
scanf("%s",Key);
Insert(H,Key);
printf("输入第%d对的第二个号码:\n",i);
scanf("%s",Key);
Insert(H,Key);
}
ScanfAndOutput(H);
return 0;
}