基于visual Studio2013解决算法导论之027hash表

本文介绍了一种使用链表解决哈希表冲突的方法,通过实现哈希表类,包括插入、删除、查找等基本操作,并提供了完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




题目

hash表,用链表来解决冲突问题


解决代码及点评

/*
哈希表 链接法解决冲突问题
*/

#include <iostream>
using namespace std;

struct Node
{
	int nValue;
	Node *pNext;
};

class HashTable
{
public:
	HashTable(int n);
	~HashTable();
	void Insert(int nValue);
	void InsertList(Node *pHead, int nValue);
	void Delete(int nValue);
	void DeleteList(Node *pHead, int nValue);
	int Search(int nValue);
	int HashFun(int nValue);
	void Print();
	void DestroyList(Node *pHead);
private:
	Node **pArr;	//指针数组
	int nSize;
};

HashTable::HashTable(int n)
{
	nSize = n;
	pArr = new Node*[nSize];
	for (int i = 0; i < nSize; i++)
	{
		pArr[i] = new Node;
		pArr[i]->pNext = NULL;
	}
}

void HashTable::DestroyList(Node *pHead)
{
	Node *pDel =  NULL;
	while(pHead->pNext)
	{
		pDel = pHead->pNext;
		pHead->pNext = pDel->pNext;
		delete pDel;
	}
	delete pHead;
	pHead = NULL;
}
HashTable::~HashTable()
{
	for (int i = 0; i < nSize; i++)
	{
		DestroyList(pArr[i]);
	}
	pArr = NULL;
}

int HashTable::HashFun(int nValue)
{
	return nValue % 10;
}

void HashTable::InsertList(Node *pHead, int nValue)
{
	Node *pCur = pHead;
	Node *pTmp = new Node;
	pTmp->nValue = nValue;
	pTmp->pNext = NULL;
	while(pCur->pNext)
	{
		if (nValue < pCur->pNext->nValue)
		{
			pTmp->pNext = pCur->pNext;
			pCur->pNext = pTmp;
			return;
		}
		else if (nValue == pCur->pNext->nValue)
		{
			return;
		}
		else
		{
			pCur = pCur->pNext;
		}
	}

	pCur->pNext = pTmp;
}

void HashTable::Insert(int nValue)
{
	int nPos = HashFun(nValue);
	Node *pTmp = new Node;
	pTmp->nValue = nValue;
	pTmp->pNext = NULL;

	if (pArr[nPos] == NULL)
	{
		pArr[nPos]->pNext = pTmp;
	}
	else
	{
		InsertList(pArr[nPos], nValue);
	}
}

void HashTable::DeleteList(Node *pHead, int nValue)
{
	Node *pCur = pHead;
	while(pCur->pNext)
	{
		if (pCur->pNext->nValue == nValue)
		{
			Node *pDel = pCur->pNext;
			pCur->pNext = pDel->pNext;
			delete pDel;
			return;
		}
		else
		{
			pCur = pCur->pNext;
		}
	}
}
void HashTable::Delete(int nValue)
{
	if (-1 == Search(nValue))
	{
		cout<<"哈希表中没有该数据!"<<endl;
		return;
	}
	int nPos = HashFun(nValue);
	DeleteList(pArr[nPos], nValue);
}

//若无该数据则返回-1
int HashTable::Search(int nValue)
{
	int nPos = HashFun(nValue);
	if (pArr[nPos]->pNext == NULL)
	{
		return -1;
	}
	else
	{
		Node *pCur = pArr[nPos]->pNext;
		while (pCur)
		{
			if (pCur->nValue == nValue)
			{
				return pCur->nValue;
			}
			else
			{
				pCur = pCur->pNext;
			}
		}
		return -1;
	}

}
void HashTable::Print()
{
	for (int i = 0; i < nSize; i++)
	{
		Node *pCur = pArr[i]->pNext;
		while(pCur)
		{
			cout<<pCur->nValue<<" ";
			pCur = pCur->pNext;
		}
		cout<<endl;
	}
}
int main()
{
	int nArr[] = {1,23,4,6,7,8,0,23,12,4556,34,2,23,5456,783,23,12,556,784,24,12};
	int nLen = sizeof(nArr) / sizeof(int);
	HashTable hTable(10);
	for (int i = 0; i < nLen; i++)
	{
		hTable.Insert(nArr[i]);
	}
	cout<<hTable.Search(122)<<endl;

	hTable.Delete(4);
	hTable.Print();
	system("pause");
	return 0;
}


码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6858815

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值