<?php
header(\'Content-type:text/html;charset=utf-8\');
class HashTable{
private $buckets;
private $size = 10;
public function __construct()
{
$this->buckets = new SplFixedArray($this->size);
}
private function hash_func($key)
{
$strlen = strlen($key);
$hash_val = 0;
for ($i=0;$i<$strlen;$i++){
$hash_val += ord($key[$i]);
}
return $hash_val%$this->size;
}
public function insert($key,$val)
{
$index = $this->hash_func($key);
if(isset($this->buckets[$index])){
$new_code = new HashNode($key,$val,$this->buckets[$index]);
}else{
$new_code = new HashNode($key,$val,null);
}
$this->buckets[$index] = $new_code;
}
public function find($key)
{
$index = $this->hash_func($key);
$current = $this->buckets[$index];
while (isset($current)) {
if ($current->key == $key){
return $current->value;
}
$current = $current->next_node;
}
return null;
}
}
class HashNode{
public $key;
public $value;
public $next_node;
public function __construct($key,$value,$next_node=null)
{
$this->key = $key;
$this->value = $value;
$this->next_node = $next_node;
}
}
$hash = new HashTable();
$hash->insert(\'value1\',\'A\');
$hash->insert(\'value12\',\'B\');
//echo $hash->find(\'value1\');
//echo $hash->find(\'value2\');
echo $hash->find(\'value1\');
echo $hash->find(\'value12\');
记录一下实例,有兴趣的可以一起研究下。。。
继续阅读与本文标签相同的文章
-
Zabbix + Cloud Alert 实践分享
2026-05-18栏目: 教程
-
阿里云容器服务通过LoadBalancer暴露IPv6服务
2026-05-18栏目: 教程
-
阿里云服务器通用网络增强型实例sn2ne 独享主机速度快 适合企业公司使用
2026-05-18栏目: 教程
-
flex布局和grid布局
2026-05-18栏目: 教程
-
语音顶会Interspeech 论文解读|Constrained output embeddings for end-to-end code-switching speech recognition with only monolingual data
2026-05-18栏目: 教程
