Hash表 拉鍊法

2021-04-02 23:10:55 字數 1533 閱讀 6709

//拉鍊法建立hash表

//hash.h

#ifndef hash_h

#define hash_h

#include

using namespace std;

struct node

string value;

node* next;

};typedef node* hashnode;

const int mult = 31;

const int table_capacity = 10000;

class hash;

#endif

//hash.cpp

#include "hash.h"

#include

using namespace std;

hash::hash(hashnode* othertable)

hash::~hash()

unsigned int hash::hash(const string& word)

return index;

}void hash::insert(const string& word)

for (hashnode p=table[index];p;p = p->next)

}//發成衝突

hashnode n=new node();

n->value=word;

n->next=table[index];

table[index]=n;

}int hash::delete(const string& word)

else

else

if(currentnode)

return -1;}}

}int hash::search(const string& word)

for (hashnode p = table[index];p!=null;p=p->next)

}return -1;

}//main.cpp

/*1.編寫乙個雜湊(hash)錶類,以字串作為作為關鍵字存放和查詢記錄,

提供公共成員函式進行雜湊表的記錄插入、查詢和刪除。

並在main函式中測試對該類的使用。

*/#include "hash.h"

#include

using namespace std;

int main(int argc,char** argv)

;hash* ht = new hash(mytable);

ht->insert("ibm");

ht->insert("聯想");

ht->insert("microsoft");

ht->insert("慧譜");

ht->insert("慧通");

ht->insert("摩托羅拉");

cout<<"ibm"<< " : "

return 0;

}

hash表之拉鍊法處理衝突

define arrlen 17 define namelen 20 define addrlen 20 typedef struct rec rec hash函式,線性定址法和餘數法結合。得到的位址為陣列的下標。count為hash表長,即模。int hash char name,int coun...

雜湊表(拉鍊法)

開雜湊法又叫鏈位址法 開鏈法 開雜湊法 首先對關鍵碼集合用雜湊函式計算雜湊位址,具有相同位址的關鍵碼歸於同一子集合,每乙個子集合稱為乙個桶,各個 桶中的元素通過乙個單鏈表鏈結起來,各鍊錶的頭結點儲存在雜湊表中。設元素的關鍵碼為37,25,14,36,49,68,57,11,雜湊表為ht 12 表的大...

hash演算法與拉鍊法解決衝突

class hashnode class hashtable private function hashfunc key return hashval this size public function insert key,value else this buckets index newnode...