-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (74 loc) · 1.75 KB
/
Copy pathindex.js
File metadata and controls
85 lines (74 loc) · 1.75 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* linkedlist.js
* a singly-linked list implementation in JS without using arrays
*/
class LinkedListNode {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor(...initialVals) {
this.root = null
this.add = this.add.bind(this)
this.has = this.has.bind(this)
if (initialVals.length) {
initialVals.forEach(val => this.add(val))
}
}
// add a new node with some data to the list
add(data) {
// FIXME: data validation? #YOLO
let n = this.root
if (n === null) {
// empty list, initialize with first node
this.root = new LinkedListNode(data)
return
}
// find tail
while (n.next !== null) {
n = n.next
}
// add to tail
n.next = new LinkedListNode(data)
return
}
// return a boolean indicating whether some data exists in a node on the list
has(data) {
// FIXME: data validation? #YOLO
let n = this.root
if (n === null) {
// empty list
return false
}
// check each node's data
while (n !== null) {
if (n.data === data) {
return true
}
n = n.next
}
return false
}
}
// tests
(() => {
const list = new LinkedList(1, 2, 3)
console.log(list.has(1)) // true
console.log(list.has(2)) // true
console.log(list.has(3)) // true
list.add(4)
list.add(5)
console.log(list.has(5)) // true
console.log(list.has(4)) // true
console.log(list.has(6)) // false
const list2 = new LinkedList
list2.add('a string')
const aFunction = str => { console.log(str) }
list2.add(aFunction)
const anObject = {}
list2.add(anObject)
console.log(list2.has('a string')) // true
console.log(list2.has(aFunction)) // true
console.log(list2.has(anObject)) // true
})()