資源簡(jiǎn)介
#include
#include
#include
using namespace std;
#define NULL 0
unsigned int key; //用來輸入/輸出文件流類
unsigned int key2; //key和key2分別是用做了電話號(hào)碼和姓名的關(guān)鍵字
int *p;
struct node //新建節(jié)點(diǎn)(用戶姓名、地址、電話號(hào)碼、指向下一個(gè)結(jié)點(diǎn)的指針 )
{
char name[8],address[20];
char num[11];
node * next;
};
typedef node* pnode;
typedef node* mingzi; //聲明了名字和電話兩個(gè)指針
node **phone;
node **nam;
node *a;
void hash(char num[11]) //以電話號(hào)碼為關(guān)鍵字建立哈希函數(shù)
{
int i = 3;
key=(int)num[2];
while(num[i]!=NULL)
{
key+=(int)num[i];
i++;
}
key=key%20;
}
void hash2(char name[8]) //姓名為關(guān)鍵字建立哈希函數(shù)
{
int i = 1;
key2=(int)name[0];
while(name[i]!=NULL)
{
key2+=(int)name[i];
i++;
}
key2=key2%20;
}
//強(qiáng)制類型轉(zhuǎn)換,將用戶名的每一個(gè)字母的ASCLL碼值相加并且除以20后的余數(shù)
node* input() //輸入節(jié)點(diǎn)信息 ,建立結(jié)點(diǎn),并將結(jié)點(diǎn)的next指針指空
{
node *temp;
temp = new node;
temp->next=NULL;
cout<<"輸入姓名:"<>temp->name;
cout<<"輸入地址:"<>temp->address;
cout<<"輸入電話:"<>temp->num;
return temp;
} //對(duì)于指針類型返回的是地址
int apend() //添加節(jié)點(diǎn)
{
node *newphone;
node *newname;
newphone=input();
newname=newphone;
newphone->next=NULL;
newname->next=NULL;
hash(newphone->num); //利用哈希函數(shù)計(jì)算出對(duì)應(yīng)關(guān)鍵字的存儲(chǔ)地址
hash2(newname->name);
newphone->next = phone[key]->next; //利用電話號(hào)碼為關(guān)鍵字插入
phone[key]->next=newphone; //是采用鏈地址法,拉鏈法處理沖突的散列表結(jié)構(gòu)
newname->next = nam[key2]->next; //利用用戶名為關(guān)鍵字插入
nam[key2]->next=newname;
return 0;
}
void create() //新建節(jié)點(diǎn)
{
int i;
phone=new pnode[20]; //動(dòng)態(tài)創(chuàng)建對(duì)象數(shù)組,C++課本P188頁(yè)
for(i=0;inext=NULL;
}
}
void create2() //新建節(jié)點(diǎn)
{
int i;
nam=new mingzi[20];
for(i=0;inext=NULL;
}
}
void list() //顯示列表
{
int i;
node *p;
for(i=0;inext;
while(p)
{
cout<name<<'_'<address<<'_'<num<next;
}
}
}
void list2() //顯示列表
{
int i;
node *p;
for(i=0;inext;
while(p)
{
代碼片段和文件信息
#include???
#include?????
#include???
using?namespace?std;
?
#define?NULL?0?
unsigned?int?key;?????????//用來輸入/輸出文件流類
unsigned?int?key2;?????????//key和key2分別是用做了電話號(hào)碼和姓名的關(guān)鍵字
int?*p;?
struct?node?//新建節(jié)點(diǎn)(用戶姓名、地址、電話號(hào)碼、指向下一個(gè)結(jié)點(diǎn)的指針?)
{?
char?name[8]address[20];?
char?num[11];?
node?*?next;?
};?
typedef?node*?pnode;????
typedef?node*?mingzi;?//聲明了名字和電話兩個(gè)指針
node?**phone;?
node?**nam;?
node?*a;?
void?hash(char?num[11])?//以電話號(hào)碼為關(guān)鍵字建立哈希函數(shù)
{???????????
int?i?=?3;?
key=(int)num[2];?
while(num[i]!=NULL)?
{?
key+=(int)num[i];?
i++;?
}?
key=key%20;?
}?
void?hash2(char?name[8])??//姓名為關(guān)鍵字建立哈希函數(shù)
{??????????????????????????
int?i?=?1;?
key2=(int)name[0];?
while(name[i]!=NULL)?
{?
key2+=(int)name[i];?
i++;?
}?
key2=key2%20;?
}?
//強(qiáng)制類型轉(zhuǎn)換,將用戶名的每一個(gè)字母的ASCLL碼值相加并且除以20后的余數(shù)
node*?input()??//輸入節(jié)點(diǎn)信息?,建立結(jié)點(diǎn),并將結(jié)點(diǎn)的next指針指空
{?
node?*temp;?
temp?=?new?node;?
temp->next=NULL;?
cout<<“輸入姓名:“< cin>>temp->name;?
cout<<“輸入地址:“< cin>>temp->address;?
cout<<“輸入電話:“< cin>>temp->num;?
return?temp;?
}???????????????????//對(duì)于指針類型返回的是地址
int?apend()?//添加節(jié)點(diǎn)?
{?
node?*newphone;?
node?*newname;?
newphone=input();?
newname=newphone;?
newphone->next=NULL;?
newname->next=NULL;?
hash(newphone->num);????????//利用哈希函數(shù)計(jì)算出對(duì)應(yīng)關(guān)鍵字的存儲(chǔ)地址
hash2(newname->name);?
newphone->next?=?phone[key]->next;?//利用電話號(hào)碼為關(guān)鍵字插入
phone[key]->next=newphone;?????????//是采用鏈地址法,拉鏈法處理沖突的散列表結(jié)構(gòu)
newname->next?=?nam[key2]->next;????//利用用戶名為關(guān)鍵字插入
nam[key2]->next=newname;?
return?0;?
}?
void?create()?//新建節(jié)點(diǎn)?
{?
int?i;?
phone=new?pnode[20];????//動(dòng)態(tài)創(chuàng)建對(duì)象數(shù)組,C++課本P188頁(yè)
for(i=0;i<20;i++)?
{?
phone[i]=new?node;?
phone[i]->next=NULL;?
}?
}?
void?create2()?//新建節(jié)點(diǎn)?
{?
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< p=p->next;?
}?
}?
}?
void?list2()?//顯示列表?
{?
int?i;?
node?*p;?
for(i=0;i<20;i++)?
{?
p=nam[i]->next;?
while(p)?
{?
cout<name<<‘_‘<address<<‘_‘<num< p=p->next;?
}?
}?
}?
void?find(char?num[11])??//在以電話號(hào)碼為關(guān)鍵字的哈希表中查找用戶信息?
{?
hash(num);?
node?*q=phone[key]->next;?
while(q!=?NULL)?
{?
if(strcmp(numq->num)==0)?
break;?
q=q->next;?
}?
if(q)?
cout<name<<“_“?<address<<“_“<num< else?cout<<“無此記錄“< }?
void?find2(char?name[8])??//在以用戶名為關(guān)鍵字的哈希表中查找用戶信息?
{?
hash2(name);?
node?*q=nam[key2]->next;?
while(q!=?NULL)?
{?
if(strcmp(nameq->name)==0)?
break;?
q=q->next;?
}?
if(q)?
cout<name<<“_“?<address<<“_“<num< else?cout<<“無此記錄“< }?
void?save()?//保存用戶信息?
{?
int?i;?
node?*p;?
for(i=0;i<20;i++)?
{?
p=phone[i]->next;?
while(p)?
{?
fstream?iiout(“out.txt“?ios::out);?//創(chuàng)建一個(gè)文件流對(duì)象:iiout
iiout<name<<“_“<address<<“_“<num< p=p->next;?
}?
}?
}?
v
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????4669??2008-09-15?20:46??哈希表實(shí)現(xiàn)通訊錄\張寶軍-0604011009-哈希表實(shí)現(xiàn)通訊錄.cpp
?????文件?????172032??2008-09-16?10:46??哈希表實(shí)現(xiàn)通訊錄\張寶軍-0604011009-哈希表實(shí)現(xiàn)通訊錄.doc
?????文件??????43008??2008-09-16?10:59??哈希表實(shí)現(xiàn)通訊錄\張寶軍-0604011009-哈希表實(shí)現(xiàn)通訊錄評(píng)分表.doc
?????目錄??????????0??2010-05-16?15:51??哈希表實(shí)現(xiàn)通訊錄
-----------?---------??----------?-----??----
???????????????219709????????????????????4
評(píng)論
共有 條評(píng)論