(C语言详解)11-散列1 电话聊天狂人(小白实现详细解释)

本博文源于浙江大学《数据结构》。电话聊天狂人是非常有趣的一道题。姥姥在小白专场里从main函数搭建、重要函数编写、模块的改变与裁剪非常仔细的说明,下面就给出详细指导:
博主在前篇博文已经将散列表的基本操作实现,可查看此篇博文:
(c语言)散列表(分离链接法)基本操作集(含测试用例)
(C语言详解)11-散列1 电话聊天狂人(小白实现详细解释)_第1张图片

下面就给出小白专场后的完整代码:

//lt-1-1.c
//分离链接方法.c
#include
#include
#include
#include

#include
#define KEYLENGTH 11
#define MAXTABLESIZE 100000
#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 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 TableSize) {
     

	return Key%TableSize;
}
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;
}

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;
	
}
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;
	}else {
     
		P->Count ++;
		return false;
	}
	
}

void DestroyTable(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);
}
//需要用到HashTable 的定义
//NextPrime 
//CreateTable
//Hash Find 
//Insert
void ScanAndOutput(HashTable H)
{
     
	int i;
	int MaxCnt  = 0;
	int PCnt = 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 > 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");
}
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);
	DestroyTable(H);
	return 0;
	
}

你可能感兴趣的:(浙大pta)