算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)

 

算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)

 

算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)

/*
 * 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
 
 

 

算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)算法导论11.4开放寻址法(除法哈希函数开放寻址处理冲突)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-07-07
  • 2021-12-16
  • 2021-10-17
  • 2021-09-24
  • 2021-09-24
猜你喜欢
  • 2022-12-23
  • 2021-06-21
  • 2021-06-11
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案