【码图】数据结构作业2——电话号码本-哈希算法

问题描述:

创建基于链地址法的hash表,并实现电话薄的管理(记录不超过20个)。
电话薄中的记录包括姓名、电话号码和地址三个数据项,创建两个hash表,关键字分别为姓名和电话号码
Hash函数自定,比如可以为姓名/电话号码部分字符求和之后对17取模
完成记录的插入、查找、显示功能

输入0:增加一条记录。

 输入1:根据输入的姓名搜索记录并输出。

 输入2:根据输入的电话搜索记录并输出。

 输入3:根据姓名查找表输出全部记录。

 输入4:根据电话查找表输出全部记录。

 输入5:退出。

代码实现: 

#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;
    }
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值