Skip to content

Commit 1b0e27a

Browse files
committed
Add getValues() method to HashTable and update LinkedList READMEs.
1 parent 46daaf5 commit 1b0e27a

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

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

+1-1
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 facilitate the traversal 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

+6-5
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-
const keys = this.getKeys();
117-
118-
return keys.map(key => this.buckets[this.hash(key)].head.value.value);
115+
return this.buckets.reduce((values, bucket) => {
116+
const bucketValues = bucket.toArray()
117+
.map((linkedListNode) => linkedListNode.value.value);
118+
return values.concat(bucketValues);
119+
}, []);
119120
}
120121
}

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

+19-1
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+
const hashTable = new HashTable();
102+
expect(hashTable.getValues()).toEqual([]);
103+
});
104+
105+
it('should get all the values in case of hash collision', () => {
106+
const hashTable = new HashTable(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

+2-1
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)