File tree Expand file tree Collapse file tree 7 files changed +55
-24
lines changed Expand file tree Collapse file tree 7 files changed +55
-24
lines changed Original file line number Diff line number Diff line change 3
3
*
4
4
* @class
5
5
* @param {any } value The value to hold.
6
+ * @param {Node|null } prev The value to hold.
7
+ * @param {Node|null } next The value to hold.
6
8
*/
7
- export default function Node ( value ) {
9
+ // eslint-disable-next-line unicorn/prevent-abbreviations
10
+ export default function Node ( value , prev , next ) {
8
11
/** @member {any} The value/key held by this node. */
9
12
this . value = value ;
10
- /** @member {Node} Pointer to previous (left) sibling */
11
- this . prev = null ;
12
- /** @member {Node} Pointer to next (right) sibling */
13
- this . next = null ;
13
+ /** @member {Node|null } Pointer to previous (left) sibling */
14
+ this . prev = prev ;
15
+ /** @member {Node|null } Pointer to next (right) sibling */
16
+ this . next = next ;
14
17
}
Original file line number Diff line number Diff line change
1
+ import assert from 'assert' ;
2
+ import Node from './Node.js' ;
3
+
4
+ /**
5
+ * Push value to list.
6
+ *
7
+ * @param {Node } z Last node of first input list (can be null).
8
+ * @param {any } value Value to push.
9
+ * @return {Node } The node at the front of the list (new node if empty, input
10
+ * node otherwise).
11
+ */
12
+ export default function _push ( z , value ) {
13
+ assert ( z instanceof Node ) ;
14
+ assert ( z . next === null ) ;
15
+ const y = new Node ( value , z , null ) ;
16
+ z . next = y ;
17
+ return y ;
18
+ }
Original file line number Diff line number Diff line change
1
+ import assert from 'assert' ;
2
+ import Node from './Node.js' ;
3
+
4
+ /**
5
+ * Unshift value to list.
6
+ *
7
+ * @param {Node } x First node of first input list (can be null).
8
+ * @param {any } value Value to unshift.
9
+ * @return {Node } The node at the front of the list (hence, the new node).
10
+ */
11
+ export default function _unshift ( x , value ) {
12
+ assert ( x instanceof Node ) ;
13
+ assert ( x . prev === null ) ;
14
+ const y = new Node ( value , null , x ) ;
15
+ x . prev = y ;
16
+ return y ;
17
+ }
Original file line number Diff line number Diff line change 1
1
import Node from './Node.js' ;
2
- import _concat from './_concat .js' ;
2
+ import _push from './_push .js' ;
3
3
4
4
/**
5
5
* Creates a list from an input iterable.
@@ -13,13 +13,11 @@ export default function from(iterable) {
13
13
14
14
if ( event . done ) return null ;
15
15
16
- const first = new Node ( event . value ) ;
16
+ const first = new Node ( event . value , null , null ) ;
17
17
let last = first ;
18
18
19
19
for ( const value of it ) {
20
- const next = new Node ( value ) ;
21
- _concat ( last , next ) ;
22
- last = next ;
20
+ last = _push ( last , value ) ;
23
21
}
24
22
25
23
return first ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export {default as _iter_fast} from './_iter_fast.js';
5
5
export { default as _last } from './_last.js' ;
6
6
export { default as _len } from './_len.js' ;
7
7
export { default as _pop } from './_pop.js' ;
8
+ export { default as _push } from './_push.js' ;
8
9
export { default as _remove } from './_remove.js' ;
9
10
export { default as _rotate_left } from './_rotate_left.js' ;
10
11
export { default as _rotate_left_modulo } from './_rotate_left_modulo.js' ;
@@ -15,6 +16,7 @@ export {default as _rotate_right_unknown_length} from './_rotate_right_unknown_l
15
16
export { default as _rotate_to } from './_rotate_to.js' ;
16
17
export { default as _shift } from './_shift.js' ;
17
18
export { default as _split } from './_split.js' ;
19
+ export { default as _unshift } from './_unshift.js' ;
18
20
export { default as concat } from './concat.js' ;
19
21
export { default as empty } from './empty.js' ;
20
22
export { default as from } from './from.js' ;
Original file line number Diff line number Diff line change 1
1
import assert from 'assert' ;
2
2
import Node from './Node.js' ;
3
- import _concat from './_concat .js' ;
3
+ import _push from './_push .js' ;
4
4
5
5
/**
6
6
* Push value to list.
@@ -12,11 +12,8 @@ import _concat from './_concat.js';
12
12
* node otherwise).
13
13
*/
14
14
export default function push ( x , z , value ) {
15
- if ( x === null ) return new Node ( value ) ;
15
+ if ( x === null ) return new Node ( value , null , null ) ;
16
16
assert ( x instanceof Node ) ;
17
- assert ( z instanceof Node ) ;
18
- assert ( z . next === null ) ;
19
- const y = new Node ( value ) ;
20
- _concat ( z , y ) ;
17
+ _push ( z , value ) ;
21
18
return x ;
22
19
}
Original file line number Diff line number Diff line change 1
- import assert from 'assert' ;
2
1
import Node from './Node.js' ;
3
- import _concat from './_concat .js' ;
2
+ import _unshift from './_unshift .js' ;
4
3
5
4
/**
6
5
* Unshift value to list.
7
6
*
8
7
* @param {Node } x First node of first input list (can be null).
9
- * @param {Object } value Value to unshift.
8
+ * @param {any } value Value to unshift.
10
9
* @return {Node } The node at the front of the list (hence, the new node).
11
10
*/
12
11
export default function unshift ( x , value ) {
13
- if ( x === null ) return new Node ( value ) ;
14
- assert ( x instanceof Node ) ;
15
- const y = new Node ( value ) ;
16
- _concat ( y , x ) ;
17
- return y ;
12
+ if ( x === null ) return new Node ( value , null , null ) ;
13
+ return _unshift ( x , value ) ;
18
14
}
You can’t perform that action at this time.
0 commit comments