11-散列1 电话聊天狂人 (25 分)

1.基本按照陈越姥姥的讲解写的代码(抄…)
2.才开始卡在List是指针,如何定义了一个数组?其实类似于 int *A可以定义数组一样,List * A也可以定义一个数组,该数组每个元素都是struct LNode类型的。

在这里插入图片描述
代码如下:

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define MaxTableSize 1000000
#define MAXD 5
const int keyLength = 11;
typedef char ElementType[keyLength+1];//字符串的结尾还有一个位置
typedef int Index;

//单链表定义 每个hash表的坑位对应一个单链表的头节点
typedef struct LNode *PtrToLNode;
struct LNode {
	int count;//同样的电话号码出现下一次,该值递增
	ElementType Data;
	PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

//散列表 结构数组定义
typedef struct slNode *HashTable;
struct slNode {//散列表节点定义
	int TableSize;//表的最大长度
	PtrToLNode heads;//指向链表头节点的数组
};
int NewPrime(int N) {
	int p = (N % 2) ? (N + 2) : (N - 1);//寻找大于N的第一个奇数
	while (p<MaxTableSize)
	{	
		int i;
		for (i = (int)sqrt(p); i > 2; i--)
			if (p%i == 0)break;
		if (i == 2)break;
		else p += 2;//对奇数判断 是否为素数
	}
	return p;
}
int Hash(int Key, int P) {
	//除留余数法
	return Key%P;
}
HashTable CreateTable(int TableSize) {
	HashTable H = new(struct slNode);
	//保证散列的最大长度为素数
	H->TableSize = NewPrime(TableSize);
	//H->heads = (List)malloc(sizeof(H->TableSize*sizeof(struct slNode)));
	H->heads = new struct LNode[H->TableSize];
	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;
}
Position Find(HashTable H, ElementType Key) {
	Index Pos = Hash(atoi(Key+keyLength-MAXD),H->TableSize);
	Position P = H->heads[Pos].Next;
	while (P&&strcmp(P->Data, Key)) {
		P = P->Next;
	}
	return P;
}
void insert(HashTable H, ElementType Key) {
	Position P = Find(H, Key);
	if (!P)
	{
		Position newcell = new (struct LNode);
		strcpy(newcell->Data, Key);
		newcell->count = 1;
		Index pos = Hash(atoi(Key + keyLength - MAXD), H->TableSize);
		newcell->Next = H->heads[pos].Next;
		H->heads[pos].Next = newcell;
	}
	else
	{
		P->count++;
	}
}
void ScanAndOutput(HashTable H) {
	int MaxCnt = 0, Pcnt = 0;
	ElementType MinPhone;
	MinPhone[0] = '\0';
	for (int i = 0; i < H->TableSize; i++)
	{	
		PtrToLNode Ptr;
		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 DestroryTable(HashTable H) {
	Position p, tmp;
	for (int  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);
}
int main() {
	int N;
	ElementType Key;
	HashTable H;
	cin >> N;
	H = CreateTable(N * 2);//创建散列表
	for (int i = 0; i < N; i++)
	{
		scanf("%s", Key);
		insert(H, Key);
		scanf("%s", Key);
		insert(H, Key);
	}
 	
	ScanAndOutput(H);
	DestroryTable(H);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值