Created
January 20, 2017 19:27
-
-
Save heatherbooker/a2c72790082c99c3d82404f385538ffe to your computer and use it in GitHub Desktop.
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
// To run: copy this file (or open it using the "Raw" button in the right-hand corner) and save it as linkedList.js | |
// Then in the terminal, run `node linkedList.js | less` to view output and compare expected to actual values. | |
class LinkedList { | |
constructor(firstItem) { | |
this.list = { | |
data: firstItem, | |
link: {} | |
}; | |
} | |
insertBefore(nodeToInsertBefore, newNode) { | |
var node = this.findPrevNode(nodeToInsertBefore); | |
var oldNode = Object.assign({}, node.link); | |
node.link = {data: newNode, link: oldNode}; | |
} | |
insertAfter(nodeToInsertAfter, newNode) { | |
var node = this.findNode(nodeToInsertAfter, this.list); | |
var oldNode = Object.assign({}, node.link); | |
node.link = {data: newNode, link: oldNode}; | |
} | |
findNode(dataAtNode, nodeToStartAt=this.list) { | |
if (nodeToStartAt.data === dataAtNode) { | |
return nodeToStartAt; | |
} | |
return this.findNode(dataAtNode, nodeToStartAt.link); | |
} | |
findPrevNode(dataAtNode, prevNode=null, nodeToStartAt=this.list) { | |
if (nodeToStartAt.data === dataAtNode) { | |
return prevNode; | |
} | |
return this.findPrevNode(dataAtNode, nodeToStartAt, nodeToStartAt.link); | |
} | |
} | |
function test() { | |
var list = new LinkedList('words'); | |
// {data: words, link: {}} | |
list.insertAfter('words', 'more words'); | |
// {data: words, link: {data: more words, link: {}}} | |
list.insertAfter('more words', 'yet more'); | |
// {data: words, link: {data: more words, link: {data: yet more, link: {}}}} | |
list.insertAfter('words', 'moar'); | |
// {data: words, link: {data: moar, link: {data: more words, link: {data: yet more, link: {}}}} | |
var foundNode = list.findNode('moar'); | |
// {data: moar, link: {data: more words, link: {data: yet more, link {}}}} | |
var shouldHaveFoundNode = {data: 'moar', link: {data: 'more words', link: {data: 'yet more', link: {}}}}; | |
console.log('searched for node where "data" is "moar"; should have found:\n', JSON.stringify(shouldHaveFoundNode, null, 2)); | |
console.log('actually found:\n', JSON.stringify(foundNode, null, 2)); | |
list.insertBefore('more words', 'WURD'); | |
// {data: words, link: {data: moar, link: {data: WURD, link: {data: more words, link: {data: yet more, link: {}}}} | |
var finalList = {data: 'words', link: {data: 'moar', link: {data: 'WURD', link: {data: 'more words', link: {data: 'yet more', link: {}}}}}}; | |
console.log('final list should look like: \n', JSON.stringify(finalList, null, 2)); | |
console.log('actual list is:\n'); | |
console.log(JSON.stringify(list.list, null, 2)); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment