主题:拉链法建立散列表
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
const int M=100;
class linkbook
{
public:
char tel[11];
string name;
string address;
linkbook *next;
void print()
{
cout<<"电话号码为";
cout<<tel<<endl;
cout<<"联系人为:"<<name<<endl;
cout<<"联系人地址为:"<<address<<endl;
}
};
class hash
{
public:
linkbook *HT[M];
linkbook *HT1[M];
void inihash()
{
for(int i=0;i<M;i++)
{
HT[i]=new linkbook;
HT[i]->next=NULL;
HT1[i]=new linkbook;
HT1[i]->next=NULL;
}
}
int H(char a[],int n)
{
int key=(a[n-2]-'0')*10+(a[n-1]-'0');
return key;
}
int H1(string name)
{
int key=name.length()*8;
return key;
}
void creattel()
{
char tel[11];
string name,address;
linkbook *s,*s1;
s=s1=new linkbook;
cout<<"输入电话号码:";
for(int i=0;i<11;i++){cin>>tel[i];}
int key=H(tel,11);
cout<<"存储地址 :"<<key<<endl;
strcpy(HT[key]->tel,tel);strcpy(HT1[key]->tel,tel);//电话号码
cout<<"输入联系人:";
cin>>name;
int key1=H1(name);cout<<"存储地址 :"<<key1<<endl;
s1->name=name;//姓
s->name=name;//名
cout<<"输入联系人地址:";
cin>>address;
s->address=address;//地
s1->address=address;//址
s->next=HT[key]->next;HT[key]->next=s;
s1->next=HT1[key1]->next;HT1[key1]->next=s1;
}
void findtel()
{
char tel[11];
cout<<"输入要查找的电话号码:";
for(int i=0;i<11;i++){cin>>tel[i];}
int key=H(tel,11);
cout<<"存储地址 :"<<key<<endl;
linkbook *p;
p=HT[key]->next;
if(p==NULL)cout<<"查找失败"<<endl;
else if(p->tel==tel)p->print();
else
{
while((p->tel!=tel)&&(p->next!=NULL))
p=p->next;
if(p->next==NULL)cout<<"查找失败"<<endl;
else
p->print();
}
}
void findname()
{
string name;
cout<<"输入要查找的联系人:";
cin>>name;
int key=H1(name);
cout<<"存储地址 :"<<key<<endl;
linkbook *p;
p=HT1[key]->next;
if(p==NULL)cout<<"查找失败"<<endl;
else if(p->name==name)p->print();
else
{
while((p->name!=name)&&(p->next!=NULL))
p=p->next;
if(p->next==NULL)cout<<"查找失败"<<endl;
else
p->print();
}
}
};
void menu(hash s)
{ s.inihash();
cout<<"---散列表的设计与实现---"<<endl;
cout<<"1 输入联系人信息"<<endl;
cout<<"2 电话查找"<<endl;
cout<<"3 姓名查找"<<endl;
cout<<"4 退出系统"<<endl;
cout<<"-------欢迎使用---------"<<endl;
cout<<"输入功能选项 :";
int option;
cin>>option;
while(1){
switch(option){
case 1:s.creattel();break;
case 2:s.findtel();break;
case 3:s.findname();break;
case 4:exit(0);break;
}
cout<<"输入功能选项 :";
cin>>option;
}
}
int main()
{
hash s;
menu(s);
}
#include<string>
#include<cstdlib>
using namespace std;
const int M=100;
class linkbook
{
public:
char tel[11];
string name;
string address;
linkbook *next;
void print()
{
cout<<"电话号码为";
cout<<tel<<endl;
cout<<"联系人为:"<<name<<endl;
cout<<"联系人地址为:"<<address<<endl;
}
};
class hash
{
public:
linkbook *HT[M];
linkbook *HT1[M];
void inihash()
{
for(int i=0;i<M;i++)
{
HT[i]=new linkbook;
HT[i]->next=NULL;
HT1[i]=new linkbook;
HT1[i]->next=NULL;
}
}
int H(char a[],int n)
{
int key=(a[n-2]-'0')*10+(a[n-1]-'0');
return key;
}
int H1(string name)
{
int key=name.length()*8;
return key;
}
void creattel()
{
char tel[11];
string name,address;
linkbook *s,*s1;
s=s1=new linkbook;
cout<<"输入电话号码:";
for(int i=0;i<11;i++){cin>>tel[i];}
int key=H(tel,11);
cout<<"存储地址 :"<<key<<endl;
strcpy(HT[key]->tel,tel);strcpy(HT1[key]->tel,tel);//电话号码
cout<<"输入联系人:";
cin>>name;
int key1=H1(name);cout<<"存储地址 :"<<key1<<endl;
s1->name=name;//姓
s->name=name;//名
cout<<"输入联系人地址:";
cin>>address;
s->address=address;//地
s1->address=address;//址
s->next=HT[key]->next;HT[key]->next=s;
s1->next=HT1[key1]->next;HT1[key1]->next=s1;
}
void findtel()
{
char tel[11];
cout<<"输入要查找的电话号码:";
for(int i=0;i<11;i++){cin>>tel[i];}
int key=H(tel,11);
cout<<"存储地址 :"<<key<<endl;
linkbook *p;
p=HT[key]->next;
if(p==NULL)cout<<"查找失败"<<endl;
else if(p->tel==tel)p->print();
else
{
while((p->tel!=tel)&&(p->next!=NULL))
p=p->next;
if(p->next==NULL)cout<<"查找失败"<<endl;
else
p->print();
}
}
void findname()
{
string name;
cout<<"输入要查找的联系人:";
cin>>name;
int key=H1(name);
cout<<"存储地址 :"<<key<<endl;
linkbook *p;
p=HT1[key]->next;
if(p==NULL)cout<<"查找失败"<<endl;
else if(p->name==name)p->print();
else
{
while((p->name!=name)&&(p->next!=NULL))
p=p->next;
if(p->next==NULL)cout<<"查找失败"<<endl;
else
p->print();
}
}
};
void menu(hash s)
{ s.inihash();
cout<<"---散列表的设计与实现---"<<endl;
cout<<"1 输入联系人信息"<<endl;
cout<<"2 电话查找"<<endl;
cout<<"3 姓名查找"<<endl;
cout<<"4 退出系统"<<endl;
cout<<"-------欢迎使用---------"<<endl;
cout<<"输入功能选项 :";
int option;
cin>>option;
while(1){
switch(option){
case 1:s.creattel();break;
case 2:s.findtel();break;
case 3:s.findname();break;
case 4:exit(0);break;
}
cout<<"输入功能选项 :";
cin>>option;
}
}
int main()
{
hash s;
menu(s);
}