将电话号码薄以文件形式保存到盘上,能够按用户名和电话号码两种形式建立哈希表并实现插入、查找、删除表中元素的功能。用C语言修改上述代码
时间: 2024-02-01 09:15:45 浏览: 106
散列表实现电话号码查找系统
4星 · 用户满意度95%
以下是一个基于哈希表的电话号码薄的C语言代码,可以按照用户名和电话号码两种方式建立哈希表,实现插入、查找、删除表中元素的功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASHSIZE 101 //哈希表大小
#define MAX_NAME_LEN 20 //最大用户名长度
#define MAX_PHONE_LEN 15 //最大电话号码长度
typedef struct PhonebookNode {
char name[MAX_NAME_LEN]; //用户名
char phone[MAX_PHONE_LEN]; //电话号码
struct PhonebookNode* next; //下一个节点
} PhonebookNode;
typedef struct HashTable {
PhonebookNode* table[HASHSIZE]; //哈希表数组
} HashTable;
//哈希函数1:根据用户名计算哈希值
unsigned int hash1(char* name) {
unsigned int hashval;
for (hashval = 0; *name != '\0'; name++) {
hashval = *name + 31 * hashval;
}
return hashval % HASHSIZE;
}
//哈希函数2:根据电话号码计算哈希值
unsigned int hash2(char* phone) {
unsigned int hashval;
for (hashval = 0; *phone != '\0'; phone++) {
hashval = *phone + 31 * hashval;
}
return hashval % HASHSIZE;
}
//插入节点到哈希表中
void insert(HashTable* ht, char* name, char* phone) {
unsigned int hashval1 = hash1(name);
unsigned int hashval2 = hash2(phone);
PhonebookNode* node = (PhonebookNode*)malloc(sizeof(PhonebookNode));
strncpy(node->name, name, MAX_NAME_LEN);
strncpy(node->phone, phone, MAX_PHONE_LEN);
node->next = NULL;
//按用户名插入
if (ht->table[hashval1] == NULL) {
ht->table[hashval1] = node;
} else {
PhonebookNode* ptr = ht->table[hashval1];
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = node;
}
//按电话号码插入
if (ht->table[hashval2] == NULL) {
ht->table[hashval2] = node;
} else {
PhonebookNode* ptr = ht->table[hashval2];
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = node;
}
}
//查找节点
PhonebookNode* lookup(HashTable* ht, char* key, int byName) {
unsigned int hashval = byName ? hash1(key) : hash2(key);
PhonebookNode* ptr = ht->table[hashval];
while (ptr != NULL) {
if (byName ? !strncmp(ptr->name, key, MAX_NAME_LEN) : !strncmp(ptr->phone, key, MAX_PHONE_LEN)) {
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
//删除节点
void delete(HashTable* ht, char* key, int byName) {
unsigned int hashval = byName ? hash1(key) : hash2(key);
PhonebookNode* ptr = ht->table[hashval];
PhonebookNode* prev = NULL;
while (ptr != NULL) {
if (byName ? !strncmp(ptr->name, key, MAX_NAME_LEN) : !strncmp(ptr->phone, key, MAX_PHONE_LEN)) {
if (prev == NULL) {
ht->table[hashval] = ptr->next;
} else {
prev->next = ptr->next;
}
free(ptr);
return;
}
prev = ptr;
ptr = ptr->next;
}
}
int main() {
HashTable ht = {0};
insert(&ht, "Alice", "123456789");
insert(&ht, "Bob", "987654321");
insert(&ht, "Charlie", "555555555");
printf("Lookup by name:\n");
PhonebookNode* node = lookup(&ht, "Alice", 1);
printf("Name: %s, Phone: %s\n", node->name, node->phone);
node = lookup(&ht, "Bob", 1);
printf("Name: %s, Phone: %s\n", node->name, node->phone);
node = lookup(&ht, "Dave", 1);
if (node == NULL) {
printf("Dave not found!\n");
}
printf("Lookup by phone:\n");
node = lookup(&ht, "123456789", 0);
printf("Name: %s, Phone: %s\n", node->name, node->phone);
node = lookup(&ht, "987654321", 0);
printf("Name: %s, Phone: %s\n", node->name, node->phone);
node = lookup(&ht, "111111111", 0);
if (node == NULL) {
printf("111111111 not found!\n");
}
printf("Delete Alice:\n");
delete(&ht, "Alice", 1);
printf("Lookup by name after delete:\n");
node = lookup(&ht, "Alice", 1);
if (node == NULL) {
printf("Alice not found!\n");
}
printf("Lookup by phone after delete:\n");
node = lookup(&ht, "123456789", 0);
if (node == NULL) {
printf("123456789 not found!\n");
}
return 0;
}
```
这段代码定义了两个哈希函数,分别根据用户名和电话号码计算哈希值。在插入节点时,分别按照用户名和电话号码的哈希值将节点插入到哈希表中。在查找和删除节点时,可以选择按照用户名或者电话号码进行操作。
阅读全文