-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathej4_3.js
More file actions
57 lines (48 loc) · 955 Bytes
/
Copy pathej4_3.js
File metadata and controls
57 lines (48 loc) · 955 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
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Se explica el concepto de listas enlazadas y se piden las siguientes funciones:
* - arrayToList(array)
* - listToArray(list)
*/
function arrayToList(array){
let list = {
value: array[array.length -1],
rest: null
};
let tmp=list;
for(let i = array.length-2 ; i >= 0 ; i--){
tmp = {
value: array[i],
rest: list
};
list = tmp;
}
return list;
}
function listToArray(list){
let array = [];
let tmp = list;
do{
array.push(tmp.value);
tmp = tmp.rest;
}while(tmp.rest != null)
array.push(tmp.value);
return array;
}
function prepend(value, list){
let tmp = listToArray(list);
tmp.push(value);
list = arrayToList(tmp);
return list;
}
function nth(ind, list){
let tmp = listToArray(list);
return tmp[ind];
}
var foo = [1,2,3,4];
var str = arrayToList(foo);
var lista = listToArray(str);
//console.log(foo);
//console.log(JSON.stringify(str));
//console.log(lista);
console.log(foo);
console.log(nth(2,str));