C语言数据结构 电话号码查询系统的设计与实现(哈希查找)

C语言数据结构实验代码如下:
//电话号码查询系统的设计与实现
#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 

#define MAX_NAME 10
#define TABLE_SIZE 10

typedef char KeyType[MAX_NAME];
typedef  long long int InfoType;

typedef struct HNode {
	KeyType name;
	InfoType tel1;
	InfoType tel2;
	InfoType QQ;
	struct HNode* next;
} HNode;

typedef HNode* HashTable[TABLE_SIZE];
//初始化hash表
void Create_HT(HashTable HT) {
	for (int i = 0; i < TABLE_SIZE; i++)
	{
		HT[i] = NULL;
	}
}
//hash函数
int hash(KeyType name) {
	int hashval = 0;
	for (int i = 0; name[i] != '\0'; i++) {
		hashval = (hashval * 33 + name[i]) % TABLE_SIZE;
		/*注解:因为它不仅计算速度很快,而且分布比较均匀。
		核心逻辑是这段代码: hash (i) = hash (i-1) * 33 + str [i]
		这个神奇的数字33用来计算哈希的效果会比其他许多常数(无论是否为质数)更有效*/
	}
	return hashval;
}
//插入新用户记录
void Insert_HT(HashTable HT) {
	HNode* p = (HNode*)malloc(sizeof(HNode));
	if (p == NULL) {
		printf("内存分配失败\n");
		return;
	}
	printf("请输入姓名:");
	scanf("%s", p->name);
	printf("请输入电话号码1:");
	scanf("%lld", &p->tel1);
	printf("请输入电话号码2:");
	scanf("%lld", &p->tel2);
	printf("请输入QQ号码:");
	scanf("%lld", &p->QQ);
	p->next = NULL;

	int address = hash(p->name);
	if (HT[address] == NULL) {
		HT[address] = p;
	}
	else {
		p->next = HT[address];
		HT[address] = p;
	}
}
//删除指定用户的记录
void Delete_HT(HashTable HT, KeyType key) {
	int address = hash(key);
	HNode* p = HT[address];
	HNode* pre = NULL;
	while (p != NULL) {
		if (strcmp(p->name, key) == 0) {
			if (pre == NULL) {
				HT[address] = p->next;
			}
			else {
				pre->next = p->next;
			}
			free(p);
			printf("删除成功\n");
			return;
		}
		pre = p;
		p = p->next;
	}
	printf("未找到对应记录\n");
}

//查找指定用户的记录
void Search_HT(HashTable HT, KeyType key) {
	int address = hash(key);
	HNode* p = HT[address];
	while (p != NULL) {
		if (strcmp(p->name, key) == 0) {
			printf("姓名:%s\t电话号码1:%lld\t电话号码2:%lld\tQQ号码:%lld\n", p->name, p->tel1, p->tel2, p->QQ);
			return;
		}
		p = p->next;
	}
	printf("未找到对应记录\n");
}
//修改指定用户的记录 
void Modify_HT(HashTable HT, KeyType key) {
	int address = hash(key);
	HNode* p = HT[address];
	while (p != NULL) {
		if (strcmp(p->name, key) == 0) {
			int temp;
			printf("需要修改的内容有\n");
			printf("1.电话号码1\n");
			printf("2.电话号码2\n");
			printf("3.QQ号码\n");
			printf("请选择所修改的序号:");
			scanf("%d", &temp);
			printf("请输入所修改的内容:");
			switch (temp) {
			case 1:
				scanf("%lld", &p->tel1);
				break;
			case 2:
				scanf("%lld", &p->tel2);
				break;
			case 3:
				scanf("%lld", &p->QQ);
				break;
			default:
				printf("操作选择有误!\n");
			}
			printf("修改成功\n");
			return;
		}
		p = p->next;
	}
	printf("未找到对应记录\n");
}
//打印所有用户的记录 
void Print_HT(HashTable HT) {
	printf("------用户记录如下------\n");
	int flag = 0;
	for (int i = 0; i < TABLE_SIZE; i++) {
		HNode* p = HT[i];
		while (p != NULL) {
			flag = 1;
			printf("姓名:%s\t电话号码1:%lld\t电话号码2:%lld\tQQ号码:%lld\n", p->name, p->tel1, p->tel2, p->QQ);
			p = p->next;
		}
	}
	if (flag == 0) {
		printf("无用户记录\n");
	}
}

//菜单函数
int OperateMenu() {
	int choice;
	printf("\n");
	printf("---------电话号码查询系统---------\n");
	printf("1. 插入新用户记录\n");
	printf("2. 删除指定用户的记录\n");
	printf("3. 查找指定用户的记录\n");
	printf("4. 修改指定用户的记录\n");
	printf("5. 打印所有用户的记录\n");
	printf("6. 退出\n");
	printf("----------------------------------\n");
	printf("请选择操作:");
	scanf("%d", &choice);
	return choice;
}

void main() {
	HashTable HT;
	Create_HT(HT);
	int choice = 1;
	while (choice != 0) {
		choice = OperateMenu();
		switch (choice) {
		case 1:
			Insert_HT(HT);
			break;
		case 2: {
			KeyType key;
			printf("请输入要删除的姓名:");
			scanf("%s", &key);
			Delete_HT(HT, key);
			break;
		}
		case 3: {
			KeyType key;
			printf("请输入要查找的姓名:");
			scanf("%s", &key);
			Search_HT(HT, key);
			break;
		}
		case 4: {
			KeyType key;
			printf("请输入要修改的姓名:");
			scanf("%s", &key);
			Modify_HT(HT, key);
			break;
		}
		case 5:
			Print_HT(HT);
			break;
		case 6:
			return;
		default:
			printf("操作选择有误!\n");
		}
	}
}

 ps.陕理工22级数据结构第四次实验记录

你可能感兴趣的:(数据结构)