雜湊表應用

2021-08-27 15:54:55 字數 4870 閱讀 4052

本文**:

雜湊表的應用(c++實現)

問題描述:設計雜湊表實現**號碼查詢系統,實現下列功能:

(1) 假定每個記錄有下列資料項:**號碼、使用者名稱、位址。

(2) 一是從資料檔案old.txt(自己現行建好)中讀入各項記錄,二是由系統隨機產生各記錄,並且把記錄儲存到new.txt檔案中以及顯示到螢幕上,記錄條數不要少於30,然後分別以**號碼和使用者名為關鍵字建立雜湊表。

(3) 分別採用偽隨機探測再雜湊法和再雜湊法解決衝突。

(4) 查詢並顯示給定**號碼的記錄;查詢並顯示給定使用者名稱的記錄。

(5) 將沒有查詢的結果儲存到結果檔案out.txt中,顯示查詢結果前,要有提示語句。

**:

// myhashtable.cpp : 定義控制台應用程式的入口點。  

設計雜湊表實現**號碼查詢系統

//說明:一是從檔案old.txt中讀取的資料自己在程式執行前建立,

// 二是由系統隨機生成資料,在程式執行由隨機數產生器生成,並且將產生的記錄儲存到 new.txt檔案。

//存在的問題:使用隨機產生的檔案,在顯示時出現亂碼

#include "stdafx.h"

#include//檔案流

#include#include using namespace std;

const int d = ;//預定再隨機數

const int hash_maxsize = 50;//雜湊表長度

//記錄資訊型別

class datainfo

;

datainfo::datainfo():name(""), phone(""), address(""), sign('0')

ostream& operator<<(ostream& out, const datainfo& datainfo) //過載輸出操作符

//存放記錄的雜湊表型別

class hashtable

;

hashtable::hashtable():length(0)//預設建構函式

openfile.close();

}

int hashtable::random(int key, int i)// 偽隨機數探測再雜湊法處理衝突

return -1;

}

void hashtable::hashname(datainfo *datainfo)//以名字為關鍵字建立雜湊表

key = key % 42;

while(value[key]->sign == '1')//有衝突

if(key == -1) exit(1);//無衝突

length++;//當前資料個數加

value[key]->name = datainfo->name;

value[key]->address = datainfo->address;

value[key]->phone = datainfo->phone;

value[key]->sign = '1';//表示該位置有值

//cout << value[key]->name << " " << value[key]->phone << " " << value[key]->address << endl;

}

int hashtable::rehash(int key, string str)// 再雜湊法處理衝突

void hashtable::hashphone(datainfo *datainfo)//以**為關鍵字建立雜湊表

key = key % 42;

while(value[key]->sign == '1')//有衝突

length++;//當前資料個數加

value[key]->name = datainfo->name;

value[key]->address = datainfo->address;

value[key]->phone = datainfo->phone;

value[key]->sign = '1';//表示該位置有值

}

void hashtable::outfile(string name, int key)//在沒有找到時輸出未找到的記錄

fout << name << endl;//將名字寫入檔案,有個問題,每次寫入的時候總是將原來的內容替換了

fout.close();

} }

void hashtable::outhash(int key)//輸出雜湊表中關鍵字碼對應的記錄

for(unsigned int i=0; i<10; i++)

cout << value[key]->phone;

for(int i=0; i<10; i++)

cout << value[key]->address << endl;

} }

void hashtable::rafile()//隨機生成檔案,並將檔案儲存在new.txt文件中

for(int j=0; j<30; j++)

fout << name << " ";//將名字寫入檔案

string phone = "";

for(int i=0; i<11; i++)//隨機生成長位的**號碼

fout << phone << " ";//將**號碼寫入檔案

string address = "";

for(int i=0; i<29; i++)//隨機生成長個字的名字

address += ',';

fout << address << endl;//將位址寫入檔案

} fout.close();

}

void hashtable::hash(char *fname, int n)//建立雜湊表

//fname是資料儲存的檔案的名稱,用於輸入資料,n是使用者選擇的查詢方式

//函式輸入資料,並根據選擇呼叫hashname或hashphone函式進行雜湊表的建立

while(!fin.eof())//按行讀入資料

i = 0;//記錄字串陣列的下標

//a-z:97-122 a-z:65-90

//本程式的姓名和位址都使用小寫字母

while((str[i] < 97) || (str[i] > 122))//讀入名字

for(; str[i]!=' '; i++)

while(str[i] == ' ')

for(int j=0; str[i]!=' '; j++,i++)//讀入**號碼

while(str[i] == ' ')

for(int j=0; str[i]!=','; j++,i++)//讀入位址

if(n == 1)

else

delete str;

delete datainfo;

}

fin.close();

}

int hashtable::findname(string name)//根據姓名查詢雜湊表中的記錄對應的關鍵碼

key = key % 42;

while((value[key]->sign == '1') && (value[key]->name != name))

return key;

}

int hashtable::findphone(string phone)//根據**查詢雜湊表中的記錄對應的關鍵碼

key = key % 42;

int j = 1;

while((value[key]->sign == '1') && (value[key]->phone != phone))

} return key;

}

void main()

do

while((ch != 1) && (ch != 2));

ht->hash(fname, ch);

while(ch == 1)

break;

case 2:

}

}

break;

default:

cout << endl << "您的輸入有誤!" << endl;

} if(choice == 0)

} while(ch == 2)

}while(phone.length() != 11);

key2 = ht->findphone(phone);

ht->outfile(phone, key2);

ht->outhash(key2);

} break;

case 2:

} }

break;

default:

cout << endl << "您的輸入有誤!" << endl;

} if(choice == 0)

} while((ch != 1) && (ch != 2))

} system("pause");

}

雜湊表應用

以下 用開雜湊裡的拉鍊法解決雜湊衝突 include include using namespace std define hashsize 3 struct movietype struct node class hashtable hashtable hashtable hashtable ha...

雜湊表應用例項

1 問題描述 針對某個集體 比如你所在的班級 中的 人名 設計乙個雜湊表,使得平均查詢長度不超過r,完成相應的建表和查表程式。2 基本要求 假設人名為中國人姓名的漢語拼音形式。待填入雜湊表的人名共有30個,取平均查詢長度的上限為2。雜湊函式用除留餘數法構造,用偽隨機探測再雜湊發處理衝突。3 資料結構...

雜湊表應用總結

處理雜湊表衝突的方法主要有除留餘數法,平方探測方法,以下兩題具體介紹兩種方法的使用方法 time limit 400 ms memory limit 65536 kib submit statistic problem description 給定的一組無重複資料的正整數,根據給定的雜湊函式建立其對...