Last active
May 16, 2016 18:30
-
-
Save selfup/511b6b682726ee22e3e6dff955fc7924 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Node { | |
constructor(data, next_node) { | |
this.data = data || null | |
this.next_node = next_node || null | |
} | |
} | |
class List { | |
constructor() { | |
this.head = new Node | |
} | |
tail(node) { | |
node = node || this.head | |
if (node.next_node === null) return node | |
return this.tail(node.next_node) | |
} | |
append(new_data) { | |
return this.tail().next_node = new Node(new_data) | |
} | |
preppend(new_data) { | |
let new_list = new List | |
new_list.append(new_data) | |
new_list.tail().next_node = this.head.next_node | |
this.head = new_list.head | |
} | |
} | |
// | |
const l = new List | |
// | |
// | |
l.append("wow") | |
l.append("lol") | |
// | |
// TEST APPEND AND TAIL WORK AS EXPECTED | |
if (l.tail().data !== "lol") { | |
console.log("TAIL TEST FAILED") | |
} else { | |
console.log("TAIL TEST PASSED") | |
} | |
// | |
l.preppend("omg") | |
// | |
// TEST PREPPEND DOES NOT CHANGE THE TAIL | |
if (l.tail().data !== "lol") { | |
console.log("TAIL TEST FAILED") | |
} else { | |
console.log("TAIL TEST PASSED") | |
} | |
// TEST PREPPEND ACTUALLY PREPENDS | |
if (l.head.next_node.data !== "omg") { | |
console.log("TAIL TEST FAILED") | |
} else { | |
console.log("TAIL TEST PASSED") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment