-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list.js
49 lines (42 loc) · 978 Bytes
/
linked-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var Queue = (function () {
var Node = function (value) {
this.value = value
this.nextNode = null;
}
var LinkedList = function () {
this.head = null
this.tail = null
}
LinkedList.prototype.appendToTail = function(el) {
if (this.tail === null) {
this.head = this.tail = new Node(el)
} else {
var node = this.tail.nextNode = new Node(el)
this.tail = node
}
};
LinkedList.prototype.unshift = function() {
if(this.head !== null) {
this.head = this.head.nextNode
}
};
LinkedList.prototype.view = function() {
cursor = this.head
string = ''
while(cursor !== null){
string += (cursor.value + ' -> ')
cursor = cursor.nextNode
}
console.log(string);
return string
};
return new LinkedList
})();
// driver code //
list = Queue
list.appendToTail("first")
list.appendToTail("second")
list.appendToTail("third")
list.unshift()
list.appendToTail("fourth")
list.view()