目录 有注释 || 无注释
有注释
#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();
}