日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > php教程 > 數(shù)據(jù)結(jié)構(gòu):散列1(分離鏈接法)

數(shù)據(jù)結(jié)構(gòu):散列1(分離鏈接法)

來源:程序員人生   發(fā)布時間:2017-03-20 09:39:27 閱讀次數(shù):4118次

散列:

將每一個鍵映照到從0到TableSize⑴這個范圍的某個數(shù),并將其放到適當?shù)膯卧小_@個映照成為散列函數(shù)

理想情況下,不同的鍵映照到不同的單元,但是現(xiàn)實中是不可能的,所以好的散列函數(shù),應(yīng)當盡可能均勻地分配鍵。

列表的大小最好是素數(shù),這個非常非常重要。


解決沖突:

沖突:如果1個元素插入時與1個已插入的元素散列到相同的值,那末就產(chǎn)生了1個沖突。

解決沖突的方法:分離鏈接法和探測法

分離鏈接法:將散列到的同1個值的所有元素保存到1個鏈表中。肯定是要分配內(nèi)存。

對List和Vector,都是使用自己實現(xiàn)的簡單模板。Vector模板的實現(xiàn) List模板的實現(xiàn)

探測法在下1章:數(shù)據(jù)結(jié)構(gòu):散列2(探測法)

先看看1些公用的函數(shù):

//通過迭代器查找相同的對象
	template<class InputIterator, class T>
	InputIterator find(InputIterator first, InputIterator last, const T& val)
	{
		while (first != last)
		{
			if (*first == val) return first;
			++first;
		}
		return last;
	}
	//是不是1個素數(shù)
	bool isPrime(int num)
	{
		if (num == 2 || num == 3)
		{
			return true;
		}
		if (num % 6 != 1 && num % 6 != 5)
		{
			return false;
		}
		for (int i = 5; i*i <= num; i += 6)
		{
			if (num % i == 0 || num % (i + 2) == 0)
			{
				return false;
			}
		}
		return true;
	}
	//查找比x大的第1個素數(shù)
	int nextPrime(int n)
	{
		bool state = isPrime(n);
		while (!state)
		{
			state = isPrime(++n);

		}
		return n;
	}
	//string的散列函數(shù)	
	int hash(const std::string& key)
	{
		int hashVal = 0;
		for (size_t i = 0; i < key.length(); i++)
		{
			hashVal = 37 * hashVal + key[i];
		}
		return hashVal;
	}
	//int的散列函數(shù)
	int hash(int key)
	{
		return key;
	}
使用分離鏈接法的代碼:

	//分離鏈接法
	template <typename HashedObj>
	class HashTable
	{
	public:
		//構(gòu)造函數(shù)
		explicit HashTable(int size = 101) :theList(size){}
		//包括某個對象
		bool contains(const HashedObj& x)const
		{
			const List<HashedObj>& whichList = theList[myhash(x)];
			return find(whichList.begin(), whichList.end(), x) != whichList.end();
		}
		//清空散列表
		void makeEmpty()
		{
			for (int i = 0; i < theList.size(); i++)
			{
				theList[i].clear();//對每一個散列表清空
			}
		}
		//插入數(shù)據(jù)
		bool insert(const HashedObj& x)
		{
			//找到散列表中對應(yīng)位置的鏈表
 			List<HashedObj>& whichList = theList[myhash(x)];
			//在鏈表中找到x
 			List<HashedObj>::iterator iter = find(whichList.begin(), whichList.end(), x);
			if (iter != whichList.end())
			{
				return false;
			}
 			whichList.push_back(x);
			++currentSize;
			//數(shù)據(jù)的大小超過了散列表的大小,因而可知,理想情況下,散列表下的鏈表應(yīng)當是只放1個數(shù)據(jù)是最好的
			if (currentSize > theList.size())
			{
				rehash();//重構(gòu)1個更加大的散列表
			}
			return true;
		}
		//刪除數(shù)據(jù)
		void remove(const HashedObj& x)
		{
			//找到鏈表
			List<HashedObj>& whichList = theList[myhash(x)];
			List<HashedObj>::iterator iter = find(whichList.begin(), whichList.end(), x);
			if (iter == whichList.end())
			{
				return false;
			}
			//刪除
			whichList.erase(iter);
			--currentSize;
			return true;
		}
	private:
		Vector<List<HashedObj>> theList;//散列表
		int currentSize;//當前數(shù)據(jù)量
		//重新構(gòu)造1個散列表
		void rehash()
		{
			//原來的散列表
 			Vector<List<HashedObj>> oldList = theList;
			//散列表的大小擴大兩倍,然后再找接下來的第1個素數(shù)
			theList.resize(nextPrime(2 * theList.size()));
			//清空散列表
			for (int i = 0; i < theList.size(); i++)
			{
				theList[i].clear();
			}
			//插入舊的數(shù)據(jù)
			currentSize = 0;
			for (int i = 0; i < oldList.size(); i++)
			{
				List<HashedObj>::iterator iter = oldList[i].begin();
				while (iter != oldList[i].end())
				{
					insert(*iter);
					iter++;
				}
			}
		}
		//計算散列數(shù)
		int myhash(const HashedObj& x)const
		{
			int hashVal = hash(x);
			hashVal %= theList.size();
			if (hashVal < 0)
			{
				hashVal += theList.size();
			}
			return hashVal;
		}
	};



生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: av中文在线 | 日本黄在线 | 一级在线| 天堂成人国产精品一区 | 欧美日韩中文字幕在线 | 欧美精品一区二区视频 | 国内av在线| 91电影在线观看 | 91在线看免费 | 欧美猛交| 91久久久久久 | 日韩三级网址 | 99精品视频在线免费观看 | 久久久亚洲综合 | 91久久精品国产 | 亚洲午夜激情电影 | 可以在线看的av | 国产99久久| 久人久人久人久久久久人 | 99在线观看视频 | 亚洲经典在线观看 | 91久久国产| 日韩欧美精品 | 精品午夜久久 | 麻豆成人久久精品二区三区小说 | 日日弄天天弄美女bbbb | 在线视频一区二区 | 九九热久久久99国产盗摄蜜臀 | 欧美成人免费在线观看 | 国产在线一二三区 | 亚洲精品国产福利 | 欧美日精品 | 免费在线看黄网站 | 成人免费视频在线观看 | 免费视频在线观看网站 | 色伊人| 一级视频在线观看免费 | 国产精品久久久久久吹潮 | 色一情一乱一伦一区二区三区 | 久久精品成人一区二区三区蜜臀 | 免费黄色在线看 |