logo资料库

散列表的设计与实现设计散列表实现电话号码查找系统。.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
#include "iostream.h" #include "string.h" #include "fstream.h" #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; 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) } void save() { cout<name<<"_" <address<<"_"<num<next; while(p) { fstream iiout("out.txt", ios::out); iiout<name<<"_"<address<<"_"<num<next; } } } void menu() { //菜单
cout<<"0.添加记录"<>sel; if(sel==3) { cout<<"9 号码查询,8 姓名查询"<>b; if(b==9) {cout<<"请输入电话号码:"<>num; cout<<"输出查找的信息:"<>name; cout<<"输出查找的信息:"<
{cout<<"请输入要添加的内容:"<
分享到:
收藏