-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaList.js
More file actions
44 lines (40 loc) · 734 Bytes
/
aList.js
File metadata and controls
44 lines (40 loc) · 734 Bytes
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
var arrayToList = function (array) {
var i = array.length - 1;
var list = {
value: array[i],
rest : null
};
i--;
while (i >= 0) {
list = {
value :array[i],
rest: list
};
i--;
}
return list;
};
var listToArray = function (list) {
var array = [];
var smallList = list;
while (smallList.rest) { //if list.rest is null this will be false
array.push(smallList.value);
smallList = smallList.rest;
}
array.push(smallList.value);
return array;
};
var prepend = function (element, list) {
return {
rest: list,
value: element
};
};
var nth = function (list, number) {
var i = 0;
while (i < number) {
list = list.rest;
i++;
}
return list.value;
};