-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg59.js
170 lines (148 loc) · 3.15 KB
/
pg59.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//pg 59 & 60
//importing SLL class
const{SLL} = require("./SLL.js");
const{Node} = require("./SLL.js")
SLL.prototype.logNodes = function(){
var runner = this.head;
while(runner){
console.log(runner.val);
runner = runner.next;
};
console.log("done");
return;
}
SLL.prototype.addNode = function(val){
if(!this.head){
this.head = new Node(val);
return this;
}
var runner = this.head;
while(runner.next){
runner = runner.next;
}
runner.next = new Node(val);
return this;
}
SLL.prototype.addFront = function(val){
var aNode = new Node(val);
var first = this.head;
this.head = aNode;
aNode.next = first;
};
SLL.prototype.contains = function(val){
var runner = this.head;
while(runner){
if(runner.val == val){
return true;
};
};
return false;
};
SLL.prototype.removeFront = function(){
// tests for an actual node in the first position and a follow up node.
if(this.head && this.head.next){
this.head = this.head.next;
return this;
};
//if there are no follow up nodes from the first one.
return null;
};
SLL.prototype.front = function(){
if(this.head){
return this.head.val;
}
else{
return null;
};
};
SLL.prototype.length = function(){
var runner = this.head;
var counter = 0;
while(runner){
counter++;
runner = runner.next;
};
return counter;
};
SLL.prototype.display = function(){
var runner = this.head;
var counter = 0;
while(runner){
console.log("Node #", counter,"has the value:", runner.val);
runner = runner.next;
counter++;
};
};
//following function combines Max, Min, and Avg...
SLL.prototype.math = function(){
var runner = this.head,
max = runner.val,
min = runner.val,
counter = 0,
sum = 0;
while(runner){
counter++;
sum += runner.val;
if(runner.val>max){
max = runner.val;
};
if(runner.val<min){
min = runner.val;
};
runner = runner.next;
};
return [max, min, (sum/counter)];
}
//min to front: find the small value and move it to the front of the list
SLL.prototype.minToFront = function(){
var runner = this.head;
var min = runner.val;
while(runner.next){
if(runner.next.val < min){
min = runner.next.val;
var minNode = runner.next;
var before = runner;
var after = runner.next.next
}
runner = runner.next;
}
var originalHead = this.head;
before.next = after;
this.head = minNode;
minNode.next = originalHead
return this;
};
//prepend a node to an slist node of a given value
SLL.prototype.prependVal = function(val, toMatch){
var runner = this.head;
while(runner){
if(runner.next.val==toMatch){
var temp = runner.next;
runner.next = new Node(val);
runner.next.next = temp;
return;
}
runner = runner.next;
};
}
//split on val: upon matching a value, split the list into a new list going forward from that new value
SLL.prototype.splitOnValue = function(match){
var runner = this.head;
while(runner.next){
if(runner.next.val == match){
var secondList = new SLL;
secondList.head = runner.next;
runner.next = null;
return secondList;
}
runner = runner.next;
}
}
var newList = new SLL;
newList.addNode(4)
newList.addNode(3)
newList.addNode(2)
newList.addNode(5)
newList.addNode(4)
newList.logNodes();
newList.logNodes();