C++实现哈希表的创建,销毁,键值插入与删除

来源:互联网 发布:淘宝网页版电脑版登录 编辑:程序博客网 时间:2024/06/05 00:27

/** HashTable.h**  Created on: Nov 19, 2015*      Author: chris*/#pragma once#include <iostream>const int numofsizes = 7;const int hashsize[] = { 11, 19, 31, 41, 53, 61, 71 };typedef int KeyType;typedef int ElemType;struct HashCell{bool null_key;KeyType key;HashCell() : null_key(true), key(0) {}};struct HashTable{HashCell * cells;int count;int sizeindex;};bool HashTableInit(HashTable & H);void HashTableDestroy(HashTable & H);int  HashTableHashKeyDirectAddr(HashTable & H, KeyType K);bool HashTableSearchKey(HashTable & H, KeyType K, int& p, int& c);bool HashTableInsertKey(HashTable & H, KeyType K);bool HashTableDeleteKey(HashTable & H, KeyType K);void HashTableDisplay(HashTable & H);



/** HashTable.cpp**  Created on: Nov 19, 2015*      Author: chris*/#include "HashTable.h"#include <iostream>using namespace std;bool HashTableInit(HashTable & H) {H.sizeindex = 0;H.count = 0;H.cells = new HashCell[hashsize[H.sizeindex]];if (!H.cells)return false;elsereturn true;}void HashTableDestroy(HashTable & H){if (H.cells) delete H.cells;}bool HashTableReconstruct(HashTable & H){if (H.sizeindex == numofsizes - 1)return false;HashTable tmp = H;// recreate.H.cells = new HashCell[hashsize[H.sizeindex + 1]];if (!H.cells) {H.cells = tmp.cells;return false;}H.count = 0;H.sizeindex += 1;// transfer.for (int i = 0; i < hashsize[tmp.sizeindex]; ++i) {if (!tmp.cells[i].null_key) {HashTableInsertKey(H, tmp.cells[i].key);}}HashTableDestroy(tmp);return true;}int HashTableHashKeyDirectAddr(HashTable & H, KeyType K){// direct address// get pos from K.int p = K % hashsize[H.sizeindex];while (p < 0)p += hashsize[H.sizeindex];return p;}void HashTableCollision(HashTable & H, int& p, int c){// taking various method to handle collision.if (c == hashsize[H.sizeindex])p = -1;elsep = (p + 1) % hashsize[H.sizeindex];}bool HashTableSearchKey(HashTable & H, KeyType K, int& p, int& c){// find K in H.// if found: p -> pos of K// if not found: p -> pos to insert K// if overflow: p -> -1p = HashTableHashKeyDirectAddr(H, K);c = 0;while ( p != -1 &&!H.cells[p].null_key &&H.cells[p].key != K ) {// handle collision.HashTableCollision(H, p, ++c);} // endw.if (p != -1 &&!H.cells[p].null_key && K == H.cells[p].key )// found.return true;else// not found.return false;}bool HashTableInsertKey(HashTable & H, KeyType K){int p = 0;int c = 0;if (HashTableSearchKey(H, K, p, c))return false;else if (c < hashsize[H.sizeindex] / 2) {// insert K to H.cells[p].H.cells[p].null_key = false;H.cells[p].key = K;++H.count;return true;} else { // reconstruct.HashTableReconstruct(H);return false;} // end else}bool HashTableDeleteKey(HashTable & H, KeyType K){int p = 0;int c = 0;if (HashTableSearchKey(H, K, p, c)) {H.cells[p].null_key = true;int next = (p + 1) % hashsize[H.sizeindex];while (!H.cells[next].null_key) {if (next != HashTableHashKeyDirectAddr(H, H.cells[next].key)) {H.cells[p] = H.cells[next];H.cells[next].null_key = true;H.cells[next].key = 0;p = next;next = (next + 1) % hashsize[H.sizeindex];} // endifelse break;} // endwreturn true;} // endifelse return false;}void HashTableDisplay(HashTable & H){cout << "Table:" << endl;for (int i = 0; i < hashsize[H.sizeindex]; ++i) {cout << H.cells[i].null_key << " ";}cout << endl;for (int i = 0; i < hashsize[H.sizeindex]; ++i) {cout << H.cells[i].key << " ";}cout << endl;}


/* * Main.cpp * *  Created on: Oct 31, 2015 *      Author: chris */#include<iostream>#include "HashTable.h"using namespace std;int main(void) {HashTable H;if (!HashTableInit(H)) {cout << "HashTable init failed." << endl;return -1;}bool running = true;while (running) {HashTableDisplay(H);int ans = 0;do{cout << "1.Insert 2.Delete 3.Search 4.exit" << endl;cin >> ans;if (1 <= ans && ans <= 4)break;} while (true);switch (ans) {case 1:{  int key = 0;  cout << "key: "; cin >> key;  if (HashTableInsertKey(H, key))  cout << "success" << endl;  else  cout << "failure" << endl;}break;case 2:{  int key = 0;  cout << "key: "; cin >> key;  if (HashTableDeleteKey(H, key))  cout << "success" << endl;  else  cout << "failure" << endl;}break;case 3:{  int key = 0;  cout << "key: "; cin >> key;  int pos = 0, coli = 0;  if (HashTableSearchKey(H, key, pos, coli))  cout << "key found at " << pos << " with " << coli << " collisions." << endl;  else  cout << "key not found." << endl;}break;case 4:running = false;break;} // endsw} // endwcout << "final state: " << endl;HashTableDisplay(H);system("pause");HashTableDestroy(H);return 0;}





1 0