模拟哈希表(hashtable)实现 代码

目录  有注释 || 无注释


有注释

#include<iostream>
#include<vector>
using namespace std;
#define P 7

template<class Type>
class HashTable;

template<class Type>
class HashNode
{
	friend class HashTable<Type>;
public:
	HashNode(Type d = Type()) : data(d), link(nullptr)
	{}
private:
	Type data;
	HashNode* link;
};

template<class Type>
class HashTable
{
public:
	HashTable()
	{
		memset(ht, 0, sizeof(ht));
	}
	~HashTable()
	{
		Clear();
	}
public:
	void Insert(const Type& v)
	{
		int index = Hash(v);
		HashNode<Type>* s = new HashNode<Type>(v); 
        //开辟一个新的节点,并把地址返回给s
		s->link = ht[index];  
        //头插,把旧节点的地址赋给新节点的link
		ht[index] = s;        
        //把新节点的地址赋给ht[index]
	}
	//
	void Remove(const Type& key)
	{
		int index = Hash(key);

		HashNode<Type>* p = ht[index];
		if (p->data == key)  //当前节点就是要删除的节点
		{                                                                                                                             
			ht[index] = p->link;
		}
		else
		{
			while (p->link != nullptr && p->link->data != key) 
            //存在下一个节点且它的值不为key
				p = p->link;
                //p后移,直到它的下一个节点不存在

			if (p->link == nullptr) //下一个节点不存在,也就是没有找到这个值
				return;

			HashNode<Type>* pre = p; 
            //找到了,这个节点在p的下一个节点,用pre记录p当前的位置
			p = p->link;  
            //p后移,指向要删除的节点
			pre->link = p->link; 
            //将pre和p的后一个节点连接

		}
		delete p; //释放p,以免内存泄漏
	}

	HashNode<Type>* Find(const Type& key)
	{
		int index = Hash(key);
		HashNode<Type>* p = ht[index];

		while (p != nullptr && p->data != key)  
        //这个节点不为空,且它的data不为key
			p = p->link;
		return p; 
        //如果找到了就返回地址,如果是空就返回空
	}


	size_t Size()const
    //计算每一个ht[index]下挂的节点
	{
		size_t count = 0;
		for (int i = 0; i < P; ++i)
		{
			HashNode<Type>* p = ht[i];
			while (p != nullptr)
			{
				count++;
				p = p->link;
			}
		}
		return count;
	}

	void Show()const
	{
		for (int i = 0; i < P; ++i)
		{
			cout << i << " : ";
			HashNode<Type>* p = ht[i];
			while (p != nullptr)
			{
				cout << p->data << "-->";
				p = p->link;
			}
			cout << "Nil." << endl;
		}
	}

	void Clear()
	{
		for (int i = 0; i < P; ++i)
		{
			HashNode<Type>* p = ht[i];
			while (p != nullptr)
			{
				ht[i] = p->link; //把p的下一个节点赋给ht[i]
				delete p;  //释放p
				p = ht[i];  //把p重新指向下一个节点
			}
		}
	}
	//
protected:
	int Hash(const Type& key)
	{
		return key % P;
	}
private:
	HashNode<Type>* ht[P];   //vector<HashNode<Type>*> htable;
};

void main()
{
	vector<int> iv = { 1, 3, 8, 15, 22, 9, 12, 20, 10 };
	HashTable<int> ht;
	for (int i = 0; i < iv.size(); ++i)
		ht.Insert(iv[i]);
	ht.Remove(12);
	HashNode<int>* p = ht.Find(80);
	ht.Show();
	cout << "Size = " << ht.Size() << endl;
    ht.clear();
}

无注释

#include<iostream>
#include<vector>
using namespace std;
#define P 7

template<class Type>
class HashTable;

template<class Type>
class HashNode
{
	friend class HashTable<Type>;
public:
	HashNode(Type d = Type()) : data(d), link(nullptr)
	{}
private:
	Type data;
	HashNode* link;
};

template<class Type>
class HashTable
{
public:
	HashTable()
	{
		memset(ht, 0, sizeof(ht));
	}
	~HashTable()
	{
		Clear();
	}

public:
	void Insert(const Type& v)
	{
		int index = Hash(v);
		HashNode<Type>* s = new HashNode<Type>(v); 
		s->link = ht[index];  
		ht[index] = s;        
	}

	void Remove(const Type& key)
	{
		int index = Hash(key);

		HashNode<Type>* p = ht[index];
		if (p->data == key)  
		{                                                                                                                             
			ht[index] = p->link;
		}
		else
		{
			while (p->link != nullptr && p->link->data != key) 
				p = p->link;
			if (p->link == nullptr) 
				return;
			HashNode<Type>* pre = p; 
			p = p->link;  
			pre->link = p->link; 
		}
		delete p; 
	}

	HashNode<Type>* Find(const Type& key)
	{
		int index = Hash(key);
		HashNode<Type>* p = ht[index];
		while (p != nullptr && p->data != key)  
			p = p->link;
		return p; 
	}


	size_t Size()const
	{
		size_t count = 0;
		for (int i = 0; i < P; ++i)
		{
			HashNode<Type>* p = ht[i];
			while (p != nullptr)
			{
				count++;
				p = p->link;
			}
		}
		return count;
	}

	void Show()const
	{
		for (int i = 0; i < P; ++i)
		{
			cout << i << " : ";
			HashNode<Type>* p = ht[i];
			while (p != nullptr)
			{
				cout << p->data << "-->";
				p = p->link;
			}
			cout << "Nil." << endl;
		}
	}

	void Clear()
	{
		for (int i = 0; i < P; ++i)
		{
			HashNode<Type>* p = ht[i];
			while (p != nullptr)
			{
				ht[i] = p->link; 
				delete p;  
				p = ht[i];  
			}
		}
	}

protected:
	int Hash(const Type& key)
	{
		return key % P;
	}
private:
	HashNode<Type>* ht[P];   //vector<HashNode<Type>*> htable;
};

void main()
{
	vector<int> iv = { 1, 3, 8, 15, 22, 9, 12, 20, 10 };
	HashTable<int> ht;
	for (int i = 0; i < iv.size(); ++i)
		ht.Insert(iv[i]);
	ht.Remove(12);
	HashNode<int>* p = ht.Find(80);
	ht.Show();
	cout << "Size = " << ht.Size() << endl;
    ht.clear();
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值