@@ -14,6 +14,7 @@ class HashTable {
1414 this . allowResize = allowResize ;
1515 this . strongHash = strongHash ;
1616 if ( custonHash ) {
17+ // eslint-disable-next-line no-underscore-dangle
1718 this . _hash = custonHash ;
1819 }
1920
@@ -108,6 +109,21 @@ class HashTable {
108109 return res ;
109110 }
110111
112+ // eslint-disable-next-line class-methods-use-this
113+ _convertNodesToSLL ( nodeCollection ) {
114+ // convert collection of nodes into a SLL
115+ let head = nodeCollection [ 0 ] ;
116+ const start = head ;
117+ let i = 1 ;
118+ while ( i < nodeCollection . length ) {
119+ head . next = nodeCollection [ i ] ;
120+ i += 1 ;
121+ head = head . next ;
122+ }
123+
124+ return start ;
125+ }
126+
111127 set ( key , value ) {
112128 // eslint-disable-next-line no-underscore-dangle
113129 const index = this . _hash ( key ) ;
@@ -143,7 +159,6 @@ class HashTable {
143159
144160 // get the SLL using the index
145161 let head = this . bucket [ index ] ;
146-
147162 // return null if the head is null
148163 if ( ! head ) {
149164 return null ;
@@ -152,17 +167,19 @@ class HashTable {
152167 // get all the values for the key to return
153168 // eslint-disable-next-line no-underscore-dangle
154169 const vals = this . _values ( index , key ) ;
155-
156- let newHead = null ; // to hold the start of the new SLL
170+ const nodes = [ ] ;
157171 while ( head !== null ) {
158- if ( head . key === key ) {
159- // we have to delete current node
160- newHead = head . next ;
172+ if ( head . key !== key ) {
173+ nodes . push ( new HashEntry ( { key : head . key , value : head . value } ) ) ;
161174 }
162175 head = head . next ;
163176 }
177+
178+ // eslint-disable-next-line no-underscore-dangle
179+ const sll = this . _convertNodesToSLL ( nodes ) ;
180+
181+ this . bucket [ index ] = sll ;
164182 // update the index with the lastest head value
165- this . bucket [ index ] = newHead ;
166183 return { key : vals } ;
167184 }
168185
@@ -171,18 +188,16 @@ class HashTable {
171188 }
172189
173190 isEmpty ( ) {
174- return this . getSize ( ) === 0 ;
191+ return this . size === 0 ;
175192 }
176193}
177194
178- // const ht = new HashTable(5);
179- // console.log('HT slots = ', ht.slot);
195+ // const ht = new HashTable(5, { allowResize: false, strongHash: false });
180196// ht.set('maroon', 'I maroon');
181197// ht.set('hello', 'I am a new value');
182- // console.log(ht.bucket);
198+ // // console.log(ht.bucket);
183199// ht.set('hell', 'Bad value');
184200// ht.set('hello', 'I am a yet another value');
185- // console.log('HT slots = ', ht.slot);
186201// ht.set('yellow', 'I am yellow');
187202
188203// // console.log(ht.get('hello'));
0 commit comments