Hash表各种基本操作

1. 头文件common.h的内容。
#include <iostream>  
#include <string>  
#include <stack>  
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>//time.h头文件 
#include <exception>
using namespace std;  
#define HASHSIZE  13

2. 头文件DataType.h的内容。存放链表与hash结构。

#include"common.h"
#ifndef _LISTNODE_
#define _LISTNODE_

struct ListNode
{
	int m_value;
	ListNode * m_pNext;
};

#endif

#ifndef _HASH_TABLE_
#define _HASH_TABLE_

struct HashTable  //创建hash结构
{
	ListNode * value[HASHSIZE];
};

#endif

3. 初始化hash表。

HashTable * InitHashTable()
{
	HashTable * pHashTable = new HashTable;
	//memset(pHashTable, 0, sizeof(HashTable));

	for (int ii=0; ii<HASHSIZE; ++ii)
		pHashTable->value[ii] = nullptr;

	cout << "InitHashTable Success ! " << endl;

	return pHashTable;
}

4. 创建Hash表。

ListNode * CreateListFromTail()  //链表的创建,尾插法,头结点内存放元素。
{
	ListNode * pHead = new ListNode;
	if (pHead == nullptr)
	{
		cout << "分配内存失败!" << endl;
		return nullptr;
	}
	pHead = nullptr;

	ListNode *pTail = new ListNode;
	pTail = pHead;

	int input;

	while (cin>>input && input != -1)
	{
		ListNode *pTmp = new ListNode;
		pTmp->m_value = input;

		if (pHead == nullptr)
			pHead = pTmp;
		else
			pTail->m_pNext = pTmp;   //尾插法
		pTail = pTmp;

		pTmp = nullptr;
	}
	if (pTail != nullptr)
		pTail->m_pNext = nullptr;

	return pHead;
}
void CreateHashTable(HashTable * &pHashTable)
{
	for (int ii=0; ii<HASHSIZE; ++ii)
	{
			pHashTable->value[ii] = CreateListFromTail();
	}
}

5. 在Hash表中查找元素。

ListNode * FindDataInHash(HashTable *pHashTable, int data)
{
	ListNode * pNode;   //如果没有分配空间,则不能够直接赋常数项。

	if (pHashTable == nullptr)
		return nullptr;

	if ( nullptr == (pNode = pHashTable->value[data % HASHSIZE]) )
	{
		return nullptr;
	}

	while (pNode != nullptr)
	{
		if (pNode->m_value == data)
			return pNode;
		pNode = pNode->m_pNext;
	}

	return nullptr;
}

6. 删除Hash表中的元素。

bool  DeleteDataFromHash(HashTable *pHashTable, int data)
{
	if (pHashTable == nullptr || pHashTable->value[data % HASHSIZE] == nullptr)
		return false;

	if (FindDataInHash(pHashTable, data) == nullptr)
		return false;

	ListNode *pNode1 = pHashTable->value[data % HASHSIZE];
	ListNode *pNode2;
	while(pNode1->m_value != data)
	{
		pNode2 = pNode1;
		pNode1 = pNode1->m_pNext;
	}
	pNode2->m_pNext = pNode1->m_pNext;

	delete pNode1;
	pNode1 = nullptr;

	return true;
}

7. 向Hash表中插入元素。

bool InsertDataIntoHash(HashTable * pHashTable, int data)
{
	if (pHashTable == nullptr)
		return false;

	ListNode *pNode;
	if (nullptr == pHashTable->value[data % HASHSIZE])
	{
		pNode = new ListNode;
		memset(pNode, 0, sizeof(ListNode));
		pNode->m_value = data;
		pHashTable->value[data % HASHSIZE] = pNode;
		return true;
	}

	if (FindDataInHash(pHashTable, data) != nullptr)  //不能存放两个完全相同的值。
	{
		cout << data << "  has already existed ! " << endl;
		return false;
	}

	pNode = pHashTable->value[data % HASHSIZE];
	while (pNode->m_pNext != nullptr)
		pNode = pNode->m_pNext;

	pNode->m_pNext  = new ListNode;
	memset(pNode->m_pNext, 0, sizeof(ListNode));
	pNode->m_pNext->m_value = data;

	return true;
}

8. 导入数组元素创建Hash表。

void ImportDataToCreateHashTable(HashTable * &pHashTable, int *arr, int len)
{
	for (int ii=0; ii<len; ++ii)
		InsertDataIntoHash(pHashTable, arr[ii]);
}

9. 打印Hash表。

void DisplayList(ListNode * L)  // 输出链表
{
	ListNode * list = L;

	while (list != nullptr)
	{
		cout << list->m_value << " -> ";
		list = list->m_pNext;
	}
	cout << "null" << endl;
}
void DisplayHashTable(HashTable *pHashTable)
{
	for (int ii=0; ii<HASHSIZE; ++ii)
		DisplayList(pHashTable->value[ii]);
}

主函数内的测试:

int main(int argc, char **argv)
{
	int arr[] = {8, 3, 7, 9, 4, 0, 5, 1, 2, 6, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 10, 0, 2, 3};
	int len = sizeof(arr)/sizeof(int);
	HashTable *pHashTable = new HashTable;
	pHashTable = InitHashTable();

	cout << "hash函数为(X%13). 请输入hash表数据,以-1结束:" << endl;
	CreateHashTable(pHashTable);

	cout << "--------------------  直接由数组导入数据 ----------------------"<< endl;
   	ImportDataToCreateHashTable(pHashTable, arr, len);
	DisplayHashTable(pHashTable);
	cout << endl;

	cout << "插入元素:"  << endl;
	InsertDataIntoHash(pHashTable, 25);
	DisplayHashTable(pHashTable);
	cout << endl;

	cout << "插入相同元素:" << endl;
	InsertDataIntoHash(pHashTable, 25);
	DisplayHashTable(pHashTable);
	cout << endl;

	cout << "删除相同元素:" << endl;
	DeleteDataFromHash(pHashTable, 25);
	DisplayHashTable(pHashTable);
	cout << endl;

	getchar();getchar();
	return 0;
}

测试结果:


猜你喜欢

转载自blog.csdn.net/weixin_38984102/article/details/79599876