/* * IA_11.4OpenAddressing.h * * Created on: Feb 13, 2015 * Author: sunyj */ #ifndef IA_11_4OPENADDRESSING_H_ #define IA_11_4OPENADDRESSING_H_ #include <stdint.h> #include <string.h> #include <iostream> class Node { public: Node() { } Node(int64_t const k, int64_t const d) : key(k), data(d) { } int64_t key; int64_t data; }; class OpenAddressingLinerProb { public: OpenAddressingLinerProb(int64_t const n) : length(n) { data = new Node[n](); memset(data, -1, n * sizeof(Node)); } int64_t HashFunc(int64_t const key) { return key % length; } Node HashFuncLinerProbSearch(int64_t const key) { for (int64_t i = 0; i < length; i++) { if (data[HashFunc(key) + i].key == key) { return data[HashFunc(key) + i]; } } return Node(); } void HashFuncLinerProbInsert(Node const x) { for (int64_t i = 0; i < length; i++) { if (data[HashFunc(x.key) + i].key == -1) { data[HashFunc(x.key) + i] = x; return ; } } } private: Node* data; int64_t length; }; #endif /* IA_11_4OPENADDRESSING_H_ */
/* * IA_11.4OpenAddressing.cpp * * Created on: Feb 12, 2015 * Author: sunyj */ #include "IA_11.4OpenAddressing.h" int main() { OpenAddressingLinerProb linertable(7); Node node2(2, 200); Node node3(5, 300); Node node9(9, 900); Node node4(4, 400); linertable.HashFuncLinerProbInsert(node2); linertable.HashFuncLinerProbInsert(node3); linertable.HashFuncLinerProbInsert(node9); linertable.HashFuncLinerProbInsert(node4); Node tmp = linertable.HashFuncLinerProbSearch(4); std::cout << tmp.data << std::endl; tmp = linertable.HashFuncLinerProbSearch(2); std::cout << tmp.data << std::endl; tmp = linertable.HashFuncLinerProbSearch(9); std::cout << tmp.data << std::endl; return 0; } // Linear probing