Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita47fbd7

Browse files
committed
Hash Table using Linear Probing 🎉
1 parent1c9e475 commita47fbd7

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
classHashTable{
2+
constructor(){
3+
this.table=newArray(137);
4+
this.values=newArray(137);
5+
}
6+
7+
betterHash(word){
8+
lettotal=0;
9+
for(leti=0;i<word.length;i++)
10+
total=37*total+word.toLowerCase().charCodeAt(i);
11+
returntotal%this.table.length;
12+
}
13+
14+
put(key,value){
15+
constpos=this.betterHash(key);
16+
while(this.table[pos]!=undefined)
17+
pos++;
18+
this.table[pos]=key;
19+
this.values[pos]=value;
20+
}
21+
22+
get(key){
23+
consthash=this.betterHash(key);
24+
for(leti=hash;this.table[hash]!=undefined;i++)
25+
if(this.table[hash]==key)
26+
break;
27+
returnthis.table[hash];
28+
}
29+
30+
showAll(){
31+
letstr="";
32+
for(leti=0;i<this.table.length;i++)
33+
if(this.table[i]!=undefined)
34+
str+=this.values[i]+" : "+this.table[i]+"\n";
35+
returnstr;
36+
}
37+
}
38+
39+
module.exports=HashTable;

‎examples/datastructures/hash-table-separate-chaining.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class HashTable {
3434

3535
showAll(){
3636
letstr="";
37-
for(leti=0;i<this.table.length&&this.table[i]!=undefined;i++)
38-
for(letj=0;j<this.table[i].length&&this.table[i][j]!=undefined;j++)
37+
for(leti=0;i<this.table.length;i++)
38+
for(letj=0;j<this.table[i].length;j++)
3939
str+=i+" : "+this.table[i]+"\n";
4040
returnstr;
4141
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
constexpect=require('chai').expect;
2+
constHashTable=require('../../examples/datastructures/hash-table-linear-probing');
3+
4+
describe('=> HASH TABLE USING LINEAR PROBING',function(){
5+
letpeople;
6+
before(function(){
7+
people=newHashTable();
8+
});
9+
10+
it('should create an Empty Hash Table',function(done){
11+
expect(people.table.length).to.deep.equal(137);
12+
done();
13+
});
14+
15+
it('should put value into the Hash Table',function(){
16+
people.put("Diana",13);
17+
people.put("Clayton",14);
18+
people.put("Raymond",47);
19+
people.put("Nadia",28);
20+
people.put("Afzar",51);
21+
expect(people.showAll()).to.equal("13 : Diana\n51 : Afzar\n14 : Clayton\n47 : Raymond\n28 : Nadia\n");
22+
});
23+
24+
it('should get value from the Hash Table',function(){
25+
expect(people.get("Nadia")).to.equal("Nadia");
26+
expect(people.get("Diana")).to.equal("Diana");
27+
expect(people.get("Azhar")).to.equal(undefined);
28+
});
29+
30+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp