Solving the problem of hash collision--open chain Method (Zipper method)

Source: Internet
Author: User

    functionHashTable () { This. Table =NewArray (137);//137--Official better set the value of the array size         This. Buildchains =Buildchains;  This. Simplehash =Simplehash;  This. Showdistro =Showdistro;  This. put =put; //This.get=get;    }    functionBuildchains () {//Core Approach         for(vari = 0; I < This. table.length; ++i) { This. table[i] =NewArray (); }    }    functionSimplehash (data) {varTotal = 0;  for(vari = 0; i < data.length; ++i) { total+=data.charcodeat (i); }        returnTotal% This. Table.length; }    functionput (data) {varpos = This. Simplehash (data); varindex = 0; if( This. table[pos][index] = =undefined) {             This. table[pos][index] =data; } Else {             while( This. table[pos][index]! =undefined) {                ++index; }             This. table[pos][index] =data; }    }    functionShowdistro () { for(vari = 0; I < This. table.length; ++i) {if( This. table[i][0]! =undefined) {document.write (i+ ": " + This. Table[i]); document.write ("<br/>"); }        }    }    varHtable =NewHashTable ();    Htable.buildchains (); varSomenames = ["David", "Jennifer", "Donnie", "Raymond", "Cynthia",            "Mike", "Clayton", "Danny", "Jonathan" ];  for(vari = 0; i < somenames.length; ++i) {htable.put (somenames[i]);    } htable.showdistro (); /*the above program operation result is: 35:cynthia 45:raymond,clayton 57:donnie 77:david 95:danny 116:mike 132:jennifer 134:jonathan*/

Solving the problem of hash collision--open chain Method (Zipper method)

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.