国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Maison php教程 PHP源碼 PHP 實現(xiàn)HASH表

PHP 實現(xiàn)HASH表

Nov 09, 2016 pm 02:43 PM

Hash 表又稱散列表,通過關(guān)鍵字Key 映射到數(shù)組中一個位置來訪問記錄

Hash 函數(shù)的作用是把任意長度的輸入,通過HASH算法變換成固定長度的輸出,該輸出就是HASH值

HASH表的時間復(fù)雜度為O(1)

下文使用直接取余法實現(xiàn)

創(chuàng)建一個hashtable

class HashTable{
     
    private $buckets;          //用于存儲數(shù)據(jù)的數(shù)組
    private $size = 12;          //記錄buckets 數(shù)組的大小
    public function __construct(){
         
        $this->buckets = new SplFixedArray($this->size);
        //SplFixedArray效率更高,也可以用一般的數(shù)組來代替
    }
     
    private function hashfunc($key){
        $strlen = strlen($key);  //返回字符串的長度
        $hashval = 0;     
        for($i = 0; $i<$strlen ; $i++){
            $hashval +=ord($key[$i]); //返回ASCII的值
        }
        return $hashval%$this->size;    //    返回取余數(shù)后的值
    }
    public function insert($key,$value){
     
    $index = $this->hashfunc($key);
    if(isset($this->buckets[$index])){
        $newNode = new HashNode($key,$value,$this->buckets[$index]);
            }else{
    $newNode = new HashNode($key,$value,null);
            }
    $this->buckets[$index] = $newNode;
        }
     
    public function find($key){
         
            $index = $this->hashfunc($key);
             
            $current = $this->buckets[$index];
            echo "</br>";
            var_dump($current);
            while(isset($current)){    //遍歷當(dāng)前鏈表
                if($current->key==$key){    //比較當(dāng)前結(jié)點關(guān)鍵字
                    return $current->value;
                }
                $current = $current->nextNode;
                //return $current->value;
            }
            return NULL;
        }
     
}

上述可能會有沖突問題,比如HASH表指向的

插入兩個元素,第二個元素的HASH值與第一個值得HASH值相同

則第二個元素將覆蓋第一個元素的值

這時我們用拉鏈法解決沖突:具有相同HASH值得字節(jié)點鏈接在同一個鏈表中。查找這個元素的時候就必須遍歷這條鏈表。

創(chuàng)建 HASHNODE

class HashNode{
            public $key;       //關(guān)鍵字
            public $value;     //數(shù)據(jù)
            public $nextNode;  //HASHNODE來存儲信息
            public function __construct($key,$value,$nextNode = NULL){
                        $this->key = $key;
                        $this->value = $value;
                        $this->nextNode = $nextNode;
                         
            }
         
}

實現(xiàn)

    $ht = new HashTable();
     $ht->insert(&#39;key1&#39;,&#39;value1&#39;);
     //$ht->insert(&#39;key12&#39;,&#39;value12&#39;);
        echo $ht->find(&#39;key1&#39;);


Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefa?on, veuillez contacter admin@php.cn

Outils d'IA chauds

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

Video Face Swap

Video Face Swap

échangez les visages dans n'importe quelle vidéo sans effort grace à notre outil d'échange de visage AI entièrement gratuit?!

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Tutoriel PHP
1502
276