hash algorithm and zipper method for conflict resolution

Source: Internet
Author: User

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.