哈希表 哈希表hash table(key,value) 的做法其实很简单,就是把Key通过一个固定的算法函数既所谓的哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间里。
而当使用哈希表进行查询的时候,就是再次使用哈希函数将key转换为对应的数组下标,并定位到该空间获取value,如此一来,就可以充分利用到数组的定位性能进行数据定位。 实现:
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct Link_Node
{
int id;
int data;
struct Link_Node *next;
}Link_Node,*Link_Node_Ptr;
typedef struct Hash_Header
{
struct Link_Node *next;
}Hash_Header,*Hash_Header_Ptr;
const int len = 25;
Hash_Header_Ptr Hash_table[len];
int hash_func(int id)
{
int pos = 0;
pos = id % len;
return pos;
}
Link_Node_Ptr init_Link_Node()
{
Link_Node_Ptr node;
node = (Link_Node_Ptr)malloc(sizeof(Link_Node));
node->next = nullptr;
return node;
}
Hash_Header_Ptr init_Hash_Header_Node()
{
Hash_Header_Ptr node;
node = (Hash_Header_Ptr)malloc(sizeof(Hash_Header));
node->next = nullptr;
return node;
}
void init_Hash_Table()
{
for (int i = 0; i < len; i++)
{
Hash_table[i] = init_Hash_Header_Node();
Hash_table[i]->next = nullptr;
}
}
void append_Link_Node(Link_Node_Ptr new_node)
{
Link_Node_Ptr node;
int pos = 0;
new_node->next = nullptr;
pos = hash_func(new_node->id);
if (Hash_table[pos]->next == nullptr)
{
Hash_table[pos]->next = new_node;
}
else
{
node = Hash_table[pos]->next;
while (node->next != nullptr)
{
node = node->next;
}
node->next = new_node;
}
}
Link_Node_Ptr search_Link_Node(int id,int *root)
{
Link_Node_Ptr node;
int pos = 0;
pos = hash_func(id);
node = Hash_table[pos]->next;
if (node == nullptr)
{
return node;
}
if (node->id == id)
{
*root = 1;
return node;
}
else
{
*root = 0;
while (node->next != nullptr)
{
if (node->next->id == id)
{
return node;
}
else
{
node = node->next;
}
}
return nullptr;
}
}
void delete_Link_Node(Link_Node_Ptr node)
{
Link_Node_Ptr delete_node;
delete_node = node->next;
node->next = delete_node->next;
free(delete_node);
delete_node = nullptr;
}
void delete_Hash_Table_Root(Link_Node_Ptr node)
{
int pos = 0;
pos = hash_func(node->id);
if (node != nullptr)
{
Hash_table[pos]->next = node->next;
free(node);
node = nullptr;
}
}
int get_Node_Num()
{
Link_Node_Ptr node;
int num = 0;
for (int i = 0; i < len; i++)
{
node = Hash_table[i]->next;
while (node!=nullptr)
{
num++;
node = node->next;
}
}
return num;
}
void drop_hash()
{
Link_Node_Ptr node;
Link_Node_Ptr node_next;
for (int i = 0; i < len; i++)
{
node = Hash_table[i]->next;
while (1)
{
if (node == nullptr)
{
Hash_table[i]->next = nullptr;
break;
}
node_next = node->next;
free(node);
node = node_next;
}
}
}
int main()
{
Link_Node_Ptr node;
init_Hash_Table();
node = init_Link_Node();
node->id = 1;
node->data = 2;
append_Link_Node(node);
node = init_Link_Node();
node->id = 10;
node->data = 205;
append_Link_Node(node);
node = init_Link_Node();
node->id = 100;
node->data = 210;
append_Link_Node(node);
node = init_Link_Node();
node->id = 10;
node->data =564;
append_Link_Node(node);
cout << "节点数:" << get_Node_Num()<<endl;
int temp = 0;
node = search_Link_Node(10,&temp);
if (node != nullptr)
{
if (temp == 0)
{
cout << node->next->id << " " << node->next->data << endl;
delete_Link_Node(node);
}
else
{
cout << node->id << " " << node->data;
delete_Link_Node(node);
}
}
else
{
cout << "查询失败" << endl;
}
return 0;
}
|