Advertisement
nher1625

linked_lists_unfinished

Jun 2nd, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayToList(arr) {
  2.   for (var i=arr.length-1; i >= 0; i--) {
  3.     var list = {value: arr[i], rest: (list || null)};
  4.   }
  5.   return list;
  6. };
  7.  
  8. function listToArray(list) {
  9.   var arr = [];
  10.   for (var node=list; node; node=node.rest)
  11.     arr.push(node.value);
  12.   return arr;
  13. };
  14.  
  15. function prepend(value, rest) {
  16.   var list = {value: null, rest: null};
  17.   list.value = value;
  18.   list.rest = rest;
  19.   return list;
  20. };
  21. function nth(list, n) {
  22.   // WHAT IN THE ACTUAL FUCK!!!
  23. };
  24. console.log(arrayToList([10, 20]));
  25. // → {value: 10, rest: {value: 20, rest: null}}
  26. console.log(listToArray(arrayToList([10, 20, 30])));
  27. // → [10, 20, 30]
  28. console.log(prepend(10, prepend(20, null)));
  29. // → {value: 10, rest: {value: 20, rest: null}}
  30. console.log(nth(arrayToList([10, 20, 30]), 1));
  31. // → 20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement