-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: Improve and simplify from, push, and unshift.
- Loading branch information
1 parent
0d49ca5
commit fe099e8
Showing
7 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import assert from 'assert'; | ||
import Node from './Node.js'; | ||
|
||
/** | ||
* Push value to list. | ||
* | ||
* @param {Node} z Last node of first input list (can be null). | ||
* @param {any} value Value to push. | ||
* @return {Node} The node at the front of the list (new node if empty, input | ||
* node otherwise). | ||
*/ | ||
export default function _push(z, value) { | ||
assert(z instanceof Node); | ||
assert(z.next === null); | ||
const y = new Node(value, z, null); | ||
z.next = y; | ||
return y; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import assert from 'assert'; | ||
import Node from './Node.js'; | ||
|
||
/** | ||
* Unshift value to list. | ||
* | ||
* @param {Node} x First node of first input list (can be null). | ||
* @param {any} value Value to unshift. | ||
* @return {Node} The node at the front of the list (hence, the new node). | ||
*/ | ||
export default function _unshift(x, value) { | ||
assert(x instanceof Node); | ||
assert(x.prev === null); | ||
const y = new Node(value, null, x); | ||
x.prev = y; | ||
return y; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import assert from 'assert'; | ||
import Node from './Node.js'; | ||
import _concat from './_concat.js'; | ||
import _unshift from './_unshift.js'; | ||
|
||
/** | ||
* Unshift value to list. | ||
* | ||
* @param {Node} x First node of first input list (can be null). | ||
* @param {Object} value Value to unshift. | ||
* @param {any} value Value to unshift. | ||
* @return {Node} The node at the front of the list (hence, the new node). | ||
*/ | ||
export default function unshift(x, value) { | ||
if (x === null) return new Node(value); | ||
assert(x instanceof Node); | ||
const y = new Node(value); | ||
_concat(y, x); | ||
return y; | ||
if (x === null) return new Node(value, null, null); | ||
return _unshift(x, value); | ||
} |