fqy131314

数据结构— —哈希表顺序实现

哈希表的初始化

bool initHash(Hash& hash, int size)
{
	//int n = HashFound(hash, key);
	hash.size = size;
	hash.list = new Sqlist[size];

	for (int i = 0; i < hash.size; i++)
	{
		hash.list[i].data = new Data[30];
		if (hash.list[i].data == NULL)
		{
			return false;
		}
		hash.list[i].length = 0;
	}

	return true;
}


哈希表的插入

bool insert(Hash& hash, int key, int value)
{
	if (found(hash, key))
	{
		return false;
	}
	else
	{
		Data D;
		D.data1 = value;
		D.key = key;

		int n = HashFound(hash, key);
		hash.list[n].data[hash.list[n].length] = D;
		hash.list[n].length++;
		return true;
	}
	
}

哈希表的查询

bool found(Hash& hash, int key)
{
	int n = HashFound(hash, key);

	for (int i = 0; i < hash.list[n].length; i++)
	{
		if (hash.list[n].data[i].key == key)
		{
			return true;
		}
	}

	return false;
}

哈希表的删除

bool Delete(Hash& hash, int key)
{
	int n = HashFound(hash, key);
	int pos = -1;

	for (int i = 0; i < hash.list[n].length; i++)
	{
		if (hash.list[n].data[i].key == key)
		{
			pos = i;
		}
	}

	if (pos != -1)
	{
		for (int j = pos; j < hash.list[n].length - 1; j++)
		{
			hash.list[n].data[j] = hash.list[n].data[j + 1];
		}
		hash.list[n].length--;
	}

	return true;
	
}

哈希表的桶查找

int HashFound(Hash& hash, int key)
{
	int n = key % hash.size;
	return n;
}

哈希表的销毁

bool Destory(Hash& hash)
{
	for (int i = 0; i < hash.size; i++)
	{
		delete (hash.list[i].data);
	}

	delete (hash.list);
	return true;
}

哈希表的代码实现

#include <iostream>
#include <Windows.h>

using namespace std;

typedef struct _Data
{
	int data1;
	int key;
}Data;

typedef struct _Sqlist
{
	Data* data;
	int length;
}Sqlist;

typedef struct _Hash
{
	int size;
	Sqlist* list;
}Hash;

bool initHash(Hash& hash, int size);
bool insert(Hash& hash, int key, int value);
int HashFound(Hash& hash, int key);
bool Delete(Hash& hash, int key);
bool Destory(Hash& hash);
bool found(Hash& hash, int key);
void Print(Hash& hash);

bool Destory(Hash& hash)
{
	for (int i = 0; i < hash.size; i++)
	{
		delete (hash.list[i].data);
	}

	delete (hash.list);
	return true;
}

bool Delete(Hash& hash, int key)
{
	int n = HashFound(hash, key);
	int pos = -1;

	for (int i = 0; i < hash.list[n].length; i++)
	{
		if (hash.list[n].data[i].key == key)
		{
			pos = i;
		}
	}

	if (pos != -1)
	{
		for (int j = pos; j < hash.list[n].length - 1; j++)
		{
			hash.list[n].data[j] = hash.list[n].data[j + 1];
		}
		hash.list[n].length--;
	}

	return true;
	
}

int HashFound(Hash& hash, int key)
{
	int n = key % hash.size;
	return n;
}

bool insert(Hash& hash, int key, int value)
{
	if (found(hash, key))
	{
		return false;
	}
	else
	{
		Data D;
		D.data1 = value;
		D.key = key;

		int n = HashFound(hash, key);
		hash.list[n].data[hash.list[n].length] = D;
		hash.list[n].length++;
		return true;
	}	
}

bool found(Hash& hash, int key)
{
	int n = HashFound(hash, key);

	for (int i = 0; i < hash.list[n].length; i++)
	{
		if (hash.list[n].data[i].key == key)
		{
			return true;
		}
	}

	return false;
}

bool initHash(Hash& hash, int size)
{
	//int n = HashFound(hash, key);
	hash.size = size;
	hash.list = new Sqlist[size];

	for (int i = 0; i < hash.size; i++)
	{
		hash.list[i].data = new Data[30];
		if (hash.list[i].data == NULL)
		{
			return false;
		}
		hash.list[i].length = 0;
	}

	return true;
}

void Print(Hash& hash)
{
	for (int i = 0; i < hash.size; i++)
	{
		for (int j = 0; j < hash.list[i].length; j++)
		{
			cout << "key:" << hash.list[i].data[j].key << " data:" << hash.list[i].data[j].data1 << endl;
		}
	}
}

int main(void)
{
	Hash hash;

	int size = 5;
	initHash(hash, size);

	insert(hash, 1, 11);
	insert(hash, 2, 12);
	insert(hash, 3, 13);
	insert(hash, 4, 14);
	Print(hash);

	Delete(hash, 2);
	Print(hash);

	Destory(hash);

	system("pasue");
	return 0;
}

posted on   会飞的鱼-blog  阅读(30)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示