php实现哈希表
来源:互联网 发布:.us域名查询 编辑:程序博客网 时间:2024/06/06 08:48
看书看到一个算法,觉得挺妙的。用拉链法解决哈希值冲突
<?php //建结点class hashNope{public $key;public $value;public $nextNope;public function __construct($key,$value,$nextNope=null){$this->key=$key;$this->value=$value;$this->nextNope=$nextNope;}}//建一个哈希表class hashTable{private $buckets;private $size=10;public function __construct(){$this->buckets=new SplFixedArray($this->size);//SplFixedArray()与array()作用一样,但效率更快}//用了最简单的哈希算法,把关键字的所有字符串加起来再取余private function hashfunc($key){$len=strlen($key);$hashval=0;for($i=0;$i<$len;$i++){$hashval+= ord($key{$i});}return $hashval % $this->size;}//插入算法public function insert($key,$value){$index= $this->hashfunc($key);if(isset($this->buckets[$index])){$nope= new hashNope($key, $value,$this->buckets[$index]);}else {$nope= new hashNope($key, $value,null);}$this->buckets[$index]=$nope;}//查找算法public function find($key){$index=$this->hashfunc($key);$current=$this->buckets[$index];while (isset($current)){ //遍历当前链表if($current->key == $key){return $current->value;}else $current =$current->nextNope;}return false;}}$ht= new hashTable();$ht->insert('key1',' value1');$ht->insert('key12', 'value2');echo $ht->find('key1');echo $ht->find('key12');?>
阅读全文
0 0
- PHP的哈希表实现
- PHP实现哈希表
- php实现哈希表
- PHP实现 拉链式哈希表
- 23.PHP的哈希表实现
- php httpRequest(php实现httpRequest)
- 【php】 AES算法php实现
- PHP Array -- PHP 实现堆栈 PHP Stack
- PHP Array -- PHP实现队列 -- PHP Queues
- PHP实现文件下载
- PHP MVC架构实现
- php中实现多线程
- PHP中实现多线程
- 用 PHP 实现下载
- Php实现实时时间
- PHP实现的记数器
- php+SQL实现分页
- asort函数php实现
- adb常用命令
- springmvc 中自定义拦截器
- HTML5新增的标签
- 我的第一次java考试题目
- PHP MySQL 增查删改
- php实现哈希表
- Merge Two Sorted Lists
- NOIP模拟(11.02)T4 赌博游戏
- 11.2考试爆炸记
- opencv----霍夫(Hough)变换:霍夫线变换,霍夫圆变换
- 画圆带动态边框圆中间显示百分比跟随边框动
- HDU 1430 DFS + 康托展开 + 映射处理 +预处理!
- Flatten Binary Tree to Linked List
- Windows线程概述