-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.js
More file actions
47 lines (45 loc) · 1.32 KB
/
Display.js
File metadata and controls
47 lines (45 loc) · 1.32 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
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
addFront(val) {
let new_node = new Node(val);
if(!this.head) {
this.head = new_node;
return this;
}
new_node.next = this.head;
this.head = new_node;
return this;
}
display(){
var string = "";
if (this.head != null){
string += this.head.data;
var jumper = this.head.next;
while(jumper !=null){
string += " & " + jumper.data; jumper = jumper.next;
// remember to return this OUTSIDE of the while loop otherwise it won't display the next.
}
return string;
}
return string;
}
}
const SLL = new LinkedList();
SLL.addFront(52);
SLL.addFront(44);
SLL.addFront(73);
SLL.addFront(666);
SLL.addFront("hello");
// SLL.addFront(goodbye);
SLL.addFront("All", "of", "this", "is" , " true ");
// ^^this only brings out the first quotes.^^
SLL.addFront("All" + " " + " of " + " this " + " is " + " true ");
console.log(SLL.display());