




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
本文格式為Word版,下載可任意編輯——哈希表的設(shè)計(jì)與實(shí)現(xiàn)
哈希表c語言
哈希表的設(shè)計(jì)與實(shí)現(xiàn)
一.正文:
設(shè)計(jì)哈希表實(shí)現(xiàn)電話號碼查詢系統(tǒng)?;疽螅?/p>
1、設(shè)每個(gè)記錄有以下數(shù)據(jù)項(xiàng):電話號碼、用戶名、地址;
2、從鍵盤輸入各記錄,分別以電話號碼和用戶名為關(guān)鍵字建立哈希表;
3、采用再哈希法解決沖突;
4、查找并顯示給定電話號碼的記錄;
5、查找并顯示給定用戶名的記錄。
二.思路:
利用哈希表實(shí)現(xiàn)電話號碼的查詢,利用數(shù)據(jù)鏈實(shí)現(xiàn)對電話記錄的增加和刪除。
三.定義函數(shù):
Typedefstruct
{
Keytypekey;
InfoTypedate;
Intcount;
}
HashTable[MaxSize];
四.概要設(shè)計(jì)
1.?dāng)?shù)據(jù)結(jié)構(gòu):
structHash
{
intcurrentSize;
structHashItem
{
intdata;
inttag;
}
table[TableSize];
};
2.基本操作:
voidInitiate(structHash*h);
//初始化操作;
intFindPos(structHashh,intx);
哈希表c語言
//查找一個(gè)數(shù)據(jù)元素的操作;
intGetValue(structHashh,inti);
//獲取一個(gè)數(shù)據(jù)元素的操作;
intInsert(structHash*h,intx);
//插入一個(gè)數(shù)據(jù)元素的操作;
intDelete(structHash*h,intx);
//刪除一個(gè)數(shù)據(jù)元素的操作;
voidPrint(structHashh);
//列表顯示數(shù)據(jù)元素的操作;
voidClear(structHash*h)
//置空全部哈希表空間的操作;
五.流程圖:
哈希表c語言
六、測試數(shù)據(jù):1、輸入0——hushandon/p>
2、輸入0xiaoshandong123456789
3、輸入1713651689952
4、輸入18hu
5、輸入2
6、輸入3
7、輸入5
8、輸入4
9、輸入6
哈希表c語言
七.程序源代碼:
#includeiostream
#includestring
#includefstream
usingnamespacestd;
#defineNULL0
unsignedintkey;
unsignedintkey2;
int*p;
structnode//建節(jié)點(diǎn)
{
charname[8],address[20];
charnum[11];
node*next;
};
typedefnode*pnode;
typedefnode*mingzi;
node**phone;
node**nam;
node*a;
voidhash(charnum[11])//哈希函數(shù)
{
inti=3;
key=(int)num[2];
while(num[i]!=NULL)
{
key+=(int)num[i];
i++;
}
key=key%20;
}
voidhash2(charname[8])//哈希函數(shù)
{
inti=1;
key2=(int)name[0];
while(name[i]!=NULL)
哈希表c語言
{
key2+=(int)name[i];
i++;
}
key2=key2%20;
}
node*input()//輸入節(jié)點(diǎn)
{
node*temp;
temp=newnode;
temp-next=NULL;
cout輸入姓名:endl;
cintemp-name;
cout輸入地址:endl;
cintemp-address;
cout輸入電話:endl;
cintemp-num;
returntemp;
}
intapend()//添加節(jié)點(diǎn)
{
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;
return0;
}
voidcreate()//新建節(jié)點(diǎn)
{
inti;
phone=newpnode[20];
for(i=0;i20;i++)
{
哈希表c語言
phone[i]=newnode;
phone[i]-next=NULL;
}
}
voidcreate2()//新建節(jié)點(diǎn)
{
inti;
nam=newmingzi[20];
for(i=0;i20;i++)
{
nam[i]=newnode;
nam[i]-next=NULL;
}
}
voidlist()//顯示列表
{
inti;
node*p;
for(i=0;i20;i++)
{
p=phone[i]-next;
while(p)
{
coutp-name'_'p-address'_'p-numendl;
p=p-next;
}
}
}
voidlist2()//顯示列表
{
inti;
node*p;
for(i=0;i20;i++)
{
p=nam[i]-next;
while(p)
{
coutp-name'_'p-address'_'p-numendl;
哈希表c語言
p=p-next;
}
}
}
voidfind(charnum[11])//查找用戶信息
{
hash(num);
node*q=phone[key]-next;
while(q!=NULL)
{
if(strcmp(num,q-num)==0)
break;
q=q-next;
}
if(q)
coutq-name_q-address_q-numendl;elsecout無此記錄endl;
}
voidfind2(charname[8])//查找用戶信息
{
hash2(name);
node*q=nam[key2]-next;
while(q!=NULL)
{
if(strcmp(name,q-name)==0)
break;
q=q-next;
}
if(q)
coutq-name_q-address_q-numendl;elsecout無此記錄endl;
}
voidsave()//保存用戶信息
{
inti;
node*p;
for(i=0;i20;i++)
{
p=phone[i]-next;
哈希表c語言
cinnum;
cout輸出查找的信息:endl;
find(num);
}
else
{cout請輸入姓名:endl;
cinname;
cout輸出查找的信息:endl;
find2(name);}}
if(sel==2)
{cout姓名散列結(jié)果:endl;
list2();}
if(sel==0)
{cout請輸入要添加的內(nèi)容:endl;
apend();}
if(sel==4)
{cout號碼散列結(jié)果:endl;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中介托管維修合同范例
- 合伙開美容院合同范例
- 產(chǎn)權(quán)收購合同范本
- 馬路車位租賃合同范本
- 參展補(bǔ)貼合同范本
- 合同范本能當(dāng)正式合同
- 公路隧道定期檢測合同范本
- 含附件合同范本
- 內(nèi)貿(mào)合同范本
- 乙房免責(zé)合同范本
- 部編人教版五年級下冊道德與法治簡答題歸納總結(jié)
- 保障性住房資格申請表
- PEP五年級上冊Unit3-字母組合ow的發(fā)音
- 籃球特色學(xué)校實(shí)施方案
- 單人心肺復(fù)蘇技術(shù)操作考核評分標(biāo)準(zhǔn)
- 水稻種子生產(chǎn)技術(shù)
- 第四章 學(xué)習(xí)心理導(dǎo)論
- 旅游政策與法規(guī)教案
- 征兵心理測試
- 2023年江蘇農(nóng)林職業(yè)技術(shù)學(xué)院高職單招(語文)試題庫含答案解析
- GB/T 15622-2005液壓缸試驗(yàn)方法
評論
0/150
提交評論