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

Commit1b0e27a

Browse files
committed
Add getValues() method to HashTable and update LinkedList READMEs.
1 parent46daaf5 commit1b0e27a

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

‎src/data-structures/doubly-linked-list/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ consists of a set of sequentially linked records called nodes. Each node contain
1212
two fields, called links, that are references to the previous and to the next
1313
node in the sequence of nodes. The beginning and ending nodes' previous and next
1414
links, respectively, point to some kind of terminator, typically a sentinel
15-
node or null, to facilitate traversal of the list. If there is only one
15+
node or null, to facilitatethetraversal of the list. If there is only one
1616
sentinel node, then the list is circularly linked via the sentinel node. It can
1717
be conceptualized as two singly linked lists formed from the same data items,
1818
but in opposite sequential orders.

‎src/data-structures/hash-table/HashTable.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ export default class HashTable {
107107
}
108108

109109
/**
110-
* Gets the list of all the stored values in the hash table in the order of
111-
* the keys map.
110+
* Gets the list of all the stored values in the hash table.
112111
*
113112
*@return {*[]}
114113
*/
115114
getValues(){
116-
constkeys=this.getKeys();
117-
118-
returnkeys.map(key=>this.buckets[this.hash(key)].head.value.value);
115+
returnthis.buckets.reduce((values,bucket)=>{
116+
constbucketValues=bucket.toArray()
117+
.map((linkedListNode)=>linkedListNode.value.value);
118+
returnvalues.concat(bucketValues);
119+
},[]);
119120
}
120121
}

‎src/data-structures/hash-table/__test__/HashTable.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ describe('HashTable', () => {
9494
hashTable.set('b','beta');
9595
hashTable.set('c','gamma');
9696

97-
expect(hashTable.getValues()).toEqual(['alpha','beta','gamma']);
97+
expect(hashTable.getValues()).toEqual(['gamma','alpha','beta']);
98+
});
99+
100+
it('should get all the values from empty hash table',()=>{
101+
consthashTable=newHashTable();
102+
expect(hashTable.getValues()).toEqual([]);
103+
});
104+
105+
it('should get all the values in case of hash collision',()=>{
106+
consthashTable=newHashTable(3);
107+
108+
// Keys `ab` and `ba` in current implementation should result in one hash (one bucket).
109+
// We need to make sure that several items from one bucket will be serialized.
110+
hashTable.set('ab','one');
111+
hashTable.set('ba','two');
112+
113+
hashTable.set('ac','three');
114+
115+
expect(hashTable.getValues()).toEqual(['one','two','three']);
98116
});
99117
});

‎src/data-structures/linked-list/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ _Read this in other languages:_
44
[_简体中文_](README.zh-CN.md),
55
[_Русский_](README.ru-RU.md),
66
[_日本語_](README.ja-JP.md),
7-
[_Português_](README.pt-BR.md)
7+
[_Português_](README.pt-BR.md),
8+
[_한국어_](README.ko-KR.md)
89

910
In computer science, a**linked list** is a linear collection
1011
of data elements, in which linear order is not given by

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp