資源簡介
1、設每個記錄有下列數據項:電話號碼、用戶名、地址;
2、從鍵盤輸入各記錄,分別以電話號碼和用戶名為關鍵字建立哈希表;
3、采用再哈希法解決沖突;
4、查找并顯示給定電話號碼的記錄;
5、查找并顯示給定用戶名的記錄。
6、在哈希函數確定的前提下,嘗試各種不同類型處理沖突的方法(至少兩種),考察平均查找長度的變化。
代碼片段和文件信息
//#include?“iostream.h“?
#include??
#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<<“輸入姓名:“< cin>>temp->name;?
cout<<“輸入地址:“< cin>>temp->address;?
cout<<“輸入電話:“< cin>>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< p=p->next;?
}?
}?
}?
void?list2()?//顯示列表?
{?
int?i;?
node?*p;?
for(i=0;i<
- 上一篇:控制方法的C語言實現
- 下一篇:角度的單位轉換,從度到度分秒,C++實現
評論
共有 條評論