<?PHPclassHashnode { Public $key; Public $value; Public $nextNode; Public function__construct ($key,$value,$nextNode=NULL) { $this-Key=$key; $this->value =$value; $this->nextnode =$nextNode; }}classhashtable{Private $buckets; Private $size= 10; Public function__construct () {$this->buckets = []; } Private functionHashfunc ($key) { $strlen=strlen($key); $hashval= 0; for($i= 0;$i<$strlen;$i++) { $hashval+=Ord($key[$i]); } return $hashval%$this-size; } Public functionInsert$key,$value) { $index=$this->hashfunc ($key); //Create a new node if(isset($this->buckets[$index])) { $newNode=NewHashnode ($key,$value,$this->buckets[$index]); } Else { $newNode=NewHashnode ($key,$value,NULL); } $this->buckets[$index] =$newNode;//Save new node } Public functionFind$key) { $index=$this->hashfunc ($key); $current=$this->buckets[$index]; while(isset($current)) { if($current-Key==$key){ return $current-value; } Var_dump($current); die; $current=$current-NextNode; } return NULL; }}
Explain:
1. Using the hash function to calculate the hash value of the keyword, the hash value to locate the hash table at the specified location
2. If this location is already occupied by another node, point the $nextnode of the new node to this node, or set the $nextnode of the new node to null
3. Save the new node to the current position of the hash table
4. Traverse the current list to compare the keyword of each node in the linked list with the Lookup keyword for equality
hash algorithm and zipper method for conflict resolution