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)