logo资料库

设计哈希表实现电话号码查询系统.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
实验五 哈希表 一、实验目的: 设计哈希表实现电话号码查询系统。基本要求: 1、设每个记录有下列数据项:电话号码、用户名、地址; 2、从键盘输入各记录,分别以电话号码和用户名为关键字建立哈希表; 3、采用再哈希法解决冲突; 4、查找并显示给定电话号码的记录; 5、查找并显示给定用户名的记录。 二、思路: 利用哈希表记录:电话号码、用户名、地址。通过判断来进行相关的增加记录、查询记 录、姓名号码排列、清空、退出等操作。 三、结构体定义: struct node { char name[8],address[20]; char num[11]; node * next; }; typedef node* pnode; typedef node* mingzi; node **phone; node **nam; node *a; 四、流程图: 判断 i I=1 添加 I=2 查询 I=3 姓名 I=4 号码 I=5 清空 I=6 退出 返回判断 i 五、代码:
//#include "iostream.h" #include #include "string.h" #include "fstream" #define NULL 0 unsigned int key; unsigned int key2; int *p; struct node { char name[8],address[20]; char num[11]; node * next; }; typedef node* pnode; typedef node* mingzi; node **phone; node **nam; node *a; using namespace std; void hash(char num[11]) { int i = 3; key=(int)num[2]; while(num[i]!=NULL) { key+=(int)num[i]; i++; } key=key%20; } void hash2(char name[8]) { int i = 1; key2=(int)name[0]; while(name[i]!=NULL) { key2+=(int)name[i]; i++; } key2=key2%20; } node* input() { node *temp; temp = new node; temp->next=NULL;
cout<<"输入姓名:"<>temp->name; cout<<"输入地址:"<>temp->address; cout<<"输入电话:"<>temp->num; return temp; } int apend() { node *newphone; node *newname; newphone=input(); newname=newphone; newphone->next=NULL; newname->next=NULL; hash(newphone->num); hash2(newname->name); newphone->next = phone[key]->next; phone[key]->next=newphone; newname->next = nam[key2]->next; nam[key2]->next=newname; return 0; } void create() { int i; phone=new pnode[20]; for(i=0;i<20;i++) { phone[i]=new node; phone[i]->next=NULL; } } void create2() { int i; nam=new mingzi[20]; for(i=0;i<20;i++) { nam[i]=new node; nam[i]->next=NULL; } } void list() { int i; node *p; for(i=0;i<20;i++) {
p=phone[i]->next; while(p) { cout<<"姓名:"<name<<"地址:" <address<<"电话号码:"<num<next; } } } void list2() { int i; node *p; for(i=0;i<20;i++) { p=nam[i]->next; while(p) { cout<<"姓名:"<name<<"地址:" <address<<"电话号码:"<num<next; } } } void find(char num[11]) { hash(num); node *q=phone[key]->next; while(q!= NULL) { if(strcmp(num,q->num)==0) break; q=q->next; } if(q) cout<<"姓名"<name<<"地址:" <address<<"电话号码:"<num<next; while(q!= NULL) { if(strcmp(name,q->name)==0) break; q=q->next; } if(q) cout<<"姓名"<name<<"地址:" <address<<"电话号码:"<num<
else cout<<"无此记录"<next; while(p) { fstream iiout("out.txt", ios::out); iiout<name<<"_"<address<<"_"<num<next; } } } void menu() { cout<<"1.添加记录"<>sel; if(sel==2) { cout<<"8 姓名查询,9 号码查询"<>b; if(b==9) { cout<<"请输入电话号码:"<>num;
cout<<"输出查找的信息:"<>name; cout<<"输出查找的信息:"<
分享到:
收藏