关于#c语言#的问题,请各位专家解答!

16**、哈希表的设计与实现
1.问题描述
设计哈希表实现电话号码查询系统.
2.基本要求
(1)设每个记录有下列数据项:电话号码、用户名、地址:
(2)从键盘输入各记录,分别以电话号码和用户名为关键字建立哈希表
(3)采用再哈希法/开放地址法/拉链法解决冲突;
(4)查找并显示给定电话号码的记录;
(5)查找并显示给定用户名的记录.
(6)在哈希函数确定的前提下,尝试各种不同类型处理冲突的方法(至少两种),考察平均查找长度
的变化.

要用 C 语言设计一个哈希表来实现电话号码查询系统,我们可以先定义一个结构来保存每条记录的数据,其中包括电话号码、用户名和地址。然后我们可以定义第二个结构来保存哈希表本身,它由一个槽数组组成,每个槽可以保存一个键值对。

接下来,我们可以实现一个哈希函数,将电话号码作为输入并返回数组中相应槽的索引。散列函数应设计为在数组中均匀分布键以避免冲突,或两个或多个键映射到同一个槽的情况。

一旦定义了结构和哈希函数,我们就可以实现将用于操作哈希表的函数。这些可能包括插入新记录、按电话号码或用户名搜索记录以及删除记录的功能。

为了处理冲突,我们可以使用几种不同方法中的一种,例如重新散列、开放寻址或拉链方法。如果原始插槽已被占用,则重新散列涉及使用不同的哈希函数来计算给定键的插槽。如果原始槽已被占用,则开放寻址涉及在阵列中搜索下一个可用槽。拉链方法涉及使用这些技术的组合来解决冲突。

一旦实现了散列表,我们就可以用它来存储电话号码、用户名和地址的记录。然后我们可以通过电话号码或用户名搜索给定的记录,并显示相应的数据。

为了评估哈希表的有效性,我们可以测量平均搜索长度,或者在表中找到给定键所需的步骤数。这将根据使用的碰撞解决方法而有所不同,因此我们可以尝试不同的方法并比较它们的性能。

代码和解释如下,望采纳

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TABLE_SIZE 100

typedef struct {
  char phone[11];   // 电话号码
  char name[50];    // 用户名
  char address[100]; // 地址
} Record;

// 哈希表结构
typedef struct {
  Record *records[TABLE_SIZE];   // 记录数组
  int size; // 当前哈希表的大小
} HashTable;

// 哈希函数,采用除留余数法
int hash(int key) {
  return key % TABLE_SIZE;
}

// 再哈希函数,采用平方取中法
int rehash(int key) {
  return (key + (key * key)) % TABLE_SIZE;
}

// 创建哈希表
HashTable *create_hash_table() {
  HashTable *table = (HashTable *)malloc(sizeof(HashTable));
  table->size = 0;
  memset(table->records, 0, sizeof(table->records));
  return table;
}

// 向哈希表中插入记录
void insert_record(HashTable *table, Record *record) {
  int key = atoi(record->phone);
  int index = hash(key);

  // 如果记录已经存在,则更新记录
  if (table->records[index] != NULL && strcmp(table->records[index]->phone, record->phone) == 0) {
    table->records[index] = record;
    return;
  }

  // 如果位置已经被占用,则使用再哈希法解决冲突
  while (table->records[index] != NULL) {
    index = rehash(index);
  }

  table->records[index] = record;
  table->size++;
}

// 根据电话号码查找记录
Record *find_record_by_phone(HashTable *table, char *phone) {
  int key = atoi(phone);
  int index = hash(key);

// 如果记录不存在,则返回 NULL
if (table->records[index] == NULL || strcmp(table->records[index]->phone, phone) != 0) {
return NULL;
}

return table->records[index];
}

// 根据用户名查找记录
Record *find_record_by_name(HashTable *table, char *name) {
// 遍历哈希表中的所有记录
for (int i = 0; i < TABLE_SIZE; i++) {
Record *record = table->records[i];
if (record != NULL && strcmp(record->name, name) == 0) {
return record;
}
}

return NULL;
}

int main() {
// 创建哈希表
HashTable *table = create_hash_table();

// 插入一些记录
Record r1 = {"123456", "Alice", "New York"};
insert_record(table, &r1);
Record r2 = {"987654", "Bob", "Los Angeles"};
insert_record(table, &r2);
Record r3 = {"456789", "Charlie", "Chicago"};
insert_record(table, &r3);

// 根据电话号码查找记录
char *phone = "987654";
Record *record = find_record_by_phone(table, phone);
if (record != NULL) {
printf("Phone: %s\n", record->phone);
printf("Name: %s\n", record->name);
printf("Address: %s\n", record->address);
} else {
printf("No record found for phone number: %s\n", phone);
}

// 根据用户名查找记录
char *name = "Charlie";
record = find_record_by_name(table, name);
if (record != NULL) {
printf("Phone: %s\n", record->phone);
printf("Name: %s\n", record->name);
printf("Address: %s\n", record->address);
} else {
printf("No record found for name: %s\n", name);
}

return 0;
}

上述代码实现了一个哈希表,可以通过电话号码和用户名来查找记录。它采用了再哈希法来解决冲突,并且使用了除留余数法和平方取中法作为哈希函。

赞一个楼上ShowMeAI

哈希表实现电话号码查询系统
非常详细,如有帮助,望采纳
https://blog.csdn.net/duchenlong/article/details/103595287