Repository navigation
Expand file tree
/
Copy pathSLLFriday.js
More file actions
96 lines (82 loc) · 1.5 KB
/
Copy pathSLLFriday.js
File metadata and controls
96 lines (82 loc) · 1.5 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
86
87
88
89
90
91
92
93
94
95
96
class Node {
constructor(element) {
this.element = element
this.next = null
}
}
class SinglyLinkedList {
constructor() {
this.head = null
this.size = 0
}
addToFront(element) {
const node = new Node(element)
var oldHead = this.head
this.head = node
node.next = oldHead
this.size++
}
addToBack(element) {
const node = new Node(element)
var cur = this.head
if (cur === null) {
this.head = node
} else {
while (cur.next) {
cur = cur.next
}
cur.next = node
}
this.size++
}
retrieve(location) {
var cur = this.head
var LLLocation = 0
while (location != LLLocation) {
cur = cur.next
LLLocation++
}
console.log(cur.element)
}
reverseList() {
var cur = this.head
var prev = cur
var tmp = this.next
while (cur) {
tmp = cur.next
cur.next = prev
prev = cur
cur = cur.next
}
// return prev
}
printList() {
var cur = this.head
var str = ''
while (cur) {
str += cur.element + ' '
cur = cur.next
}
console.log(str)
}
printIsEmpty() {
const empty = this.head === null ? true : false
console.log(empty)
}
printSize() {
console.log(this.size)
}
}
const ll = new SinglyLinkedList()
// ll.printIsEmpty()
ll.addToBack(3)
ll.addToBack(3)
ll.addToFront(5)
ll.addToFront(7)
ll.printList()
// ll.printSize()
// ll.printIsEmpty()
// ll.retrieve(1)
ll.reverseList()
// ll.printSize()
ll.printList()