Testing Add To Tail and Add To Head
https://subscription.packtpub.com/video/programming/9781800206878/p2/video2_7/-testing-add-to-tail-and-add-to-head
by Dominic Myers
JavaScript
function LinkedList() {
this.head = null;
this.tail = null;
}
function Node(value, next, prev) {
this.value = value;
this.next = next;
this.prev = prev;
}
LinkedList.prototype.addToHead = function(value) {
var newNode = new Node(value, this.head, null);
if (this.head) this.head.prev = newNode;
else this.tail = newNode;
this.head = newNode;
}
LinkedList.prototype.addToTail = function(value) {
var newNode = new Node(value, null, this.tail);
if (this.tail) this.tail.next = newNode;
else this.head = newNode;
this.tail = newNode;
}
var myLL = new LinkedList();
myLL.addToTail(10)
console.log(JSON.stringify(myLL))
myLL.addToTail(20)
myLL.addToTail(30)
myLL.addToHead(100)
console.log(myLL.tail.prev.prev.prev)