问题描述:
创建基于链地址法的hash表,并实现电话薄的管理(记录不超过20个)。 电话薄中的记录包括姓名、电话号码和地址三个数据项,创建两个hash表,关键字分别为姓名和电话号码 Hash函数自定,比如可以为姓名/电话号码部分字符求和之后对17取模 完成记录的插入、查找、显示功能
|
代码实现:
#include<iostream>
#include<cstring>
using namespace std;
typedef struct node{
char name[8];
char address[20];
char tel[11];
struct node *next;
}Person;
typedef struct hash{
Person **data;
int count;
}Hash;
void create_Hash(Hash* table);//初始化
void insert_node(Hash* name_table, Hash* tel_table, Person *head,Person *person,Person *tail);//0
bool search_name_by_list(Person *head, char name[]);//根据输入的姓名搜索记录并输出
bool search_tel_by_list(Person *head, char tel[]);//根据输入的电话搜索记录并输出
bool search_name_by_hash(Hash *nametable, char name[]);//根据姓名查找表输出全部记录
bool search_tel_by_hash(Hash *teltable, char tel[]);//根据电话查找表输出全部记录
int get_name_key(char name[]) {//哈希函数
return (name[0] + name[1]) % 17;
}
int get_tel_key(char tel[]) {//哈希函数
return((tel[10] - '0') * 10 + (tel[11] - '0')) % 17;
}
int main(){
Hash name_table,tel_table;
char name[8],tel[11];
bool flag=true;
int choice;
Person *person,*head=new Person,*tail=head;
head->next=NULL;
create_Hash(&name_table);
create_Hash(&tel_table);
while(flag){
cin>>choice;
switch(choice){
case 0://增加一条记录
person=new Person;
cin>>person->name>>person->address>>person->tel;
person->next=NULL;
insert_node(&name_table, &tel_table,head,person,tail);
break;
case 1://根据输入的姓名搜索记录并输出
cin>>name;
if(!search_name_by_list(head, name)){
cout<<"NULL";
}
break;
case 2://根据输入的电话搜索记录并输出
cin>>tel;
if(!search_tel_by_list(head, tel)){
cout<<"NULL";
}
break;
case 3://根据姓名查找表输出全部记录
cin>>name;
if(!search_name_by_hash(&name_table, name)){
cout<<"NULL";
}
break;
case 4://根据电话查找表输出全部记录
cin>>tel;
if(!search_tel_by_hash(&tel_table, tel)){
cout<<"NULL";
}
break;
case 5://退出
flag=false;
break;
}
}
delete []name_table.data;
delete []tel_table.data;
tail=head->next;
while(tail){
delete head;
head=tail;
tail=tail->next;
}
delete head;
return 0;
}
void create_Hash(Hash* table){
table->data=new Person*[17];
table->count=0;
}
void insert_node(Hash* name_table, Hash* tel_table, Person *head,Person *person,Person *tail){
//单链表的插入
tail->next=person;
tail=person;
//哈希表的插入
int name_key=get_name_key(person->name);
if(name_table->data[name_key]){
Person *p=name_table->data[name_key];
while(p->next){
p=p->next;
}
p->next=person;
}
else{
name_table->data[name_key]=person;
}
name_table->count++;
int tel_key=get_tel_key(person->tel);
if(tel_table->data[tel_key]){
Person *p=tel_table->data[tel_key];
while(p->next){
p=p->next;
}
p->next=person;
}
else{
tel_table->data[tel_key]=person;
}
tel_table->count++;
}
bool search_name_by_list(Person *head, char name[]){
Person *p=head->next;
while(p){
if(strcmp(p->name,name)==0){
cout<<p->name<<' '<<p->address<<' '<<p->tel;
return true;
}
else{
p=p->next;
}
}
return false;
}
bool search_tel_by_list(Person *head, char tel[]){
Person *p=head->next;
while(p){
if(strcmp(p->tel,tel)==0){
cout<<p->name<<' '<<p->address<<' '<<p->tel;
return true;
}
else{
p=p->next;
}
}
return false;
}
bool search_name_by_hash(Hash *nametable,char name[]){
int name_key=get_name_key(name);
Person *p;
p=nametable->data[name_key];
if(p){
while(strcmp(p->name,name)!=0&&p){
p=p->next;
}
if(p){
cout<<p->name<<' '<<p->address<<' '<<p->tel;
return true;
}
else{
return false;
}
}
else{
return false;
}
}
bool search_tel_by_hash(Hash *teltable, char tel[]){
int tel_key=get_tel_key(tel);
Person *p;
p=teltable->data[tel_key];
if(p){
while(strcmp(p->tel,tel)!=0&&p){
p=p->next;
}
if(p){
cout<<p->name<<' '<<p->address<<' '<<p->tel;
return true;
}
else{
return false;
}
}
else{
return false;
}
}