Last active
September 5, 2017 18:56
-
-
Save selfup/0a7ccd05823c628981545fc9d2a281d1 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 = null, next_node = null) { | |
this.data = data; | |
this.next_node = next_node; | |
} | |
} | |
class List { | |
constructor() { | |
this.head = new Node(); | |
} | |
tail(node = this.head) { | |
return (!node.next_node) ? node : this.tail(node.next_node); | |
} | |
append(data) { | |
this.tail().next_node = new Node(data); | |
} | |
preppend(data) { | |
const oldList = this.head.next_node; | |
this.head.next_node = new Node(data); | |
this.tail().next_node = oldList; | |
} | |
count(node = this.head, count = -1) { | |
count += 1; | |
(!node.next_node) ? count : this.count(node.next_node, count); | |
} | |
reset() { | |
this.head.next_node = null; | |
} | |
} | |
const list = new List(); | |
list.append("ok"); | |
list.preppend("wow"); | |
list.append("omg"); | |
list.preppend("lol"); | |
console.log("LIST", list); | |
console.log("HEAD", list.head); | |
console.log("TAIL", list.tail()); | |
console.log("COUNT", list.count()); | |
list.reset(); | |
console.log("RESET - LIST", list); | |
console.log("RESET - HEAD", list.head); | |
console.log("RESET - TAIL", list.tail()); | |
console.log("RESET - COUNT", list.count()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment