-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patha_list.js
More file actions
30 lines (26 loc) · 676 Bytes
/
a_list.js
File metadata and controls
30 lines (26 loc) · 676 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
function arrayToList(array) {
let list = null;
for (let i = 0; i < array.length; i++) {
list = { value: array[i], rest: list };
}
return list;
}
function listToArray(list) {
let array = [];
for (let node = list; node; node = node.rest) {
array.push(node.value);
}
return array;
}
function prepend(value, list) {
return { value, rest: list };
}
function nth(list, n) {
if (!list) return undefined;
else if (n == 0) return list.value;
else return nth(list.rest, n - 1);
}
console.log(arrayToList([10, 20, 30]));
console.log(listToArray(arrayToList([10, 20, 30])));
console.log(prepend(10, prepend(20, null)));
console.log(nth(arrayToList([10, 20, 30]), 1));