fork download
  1. function println(value)
  2. {
  3. java.lang.System.out.println(value);
  4. }
  5.  
  6. function Node(value, next)
  7. {
  8. this.value = value;
  9. this.next = next;
  10. }
  11.  
  12. function List()
  13. {
  14. this.tail = List.nil;
  15. }
  16.  
  17. List.nil = new List(0, null);
  18. List.nil.next = List.nil;
  19.  
  20. List.prototype.add = function(value)
  21. {
  22. this.tail.next = new Node(value, this.tail.next);
  23. this.tail = this.tail.next;
  24. };
  25.  
  26. List.prototype.foreach = function(a)
  27. {
  28. for (var n = this.tail.next.next; n != List.nil; n = n.next)
  29. {
  30. a(n);
  31. }
  32. };
  33.  
  34. List.prototype.sort = function(c)
  35. {
  36. var a = [];
  37. this.foreach(function(n)
  38. {
  39. a.push(n.value);
  40. });
  41. a.sort(c);
  42. a = a.reverse();
  43. this.foreach(function(n)
  44. {
  45. n.value = a.pop();
  46. });
  47. };
  48.  
  49. function main()
  50. {
  51. var list = new List();
  52. for (var i = 0; i < 10; i++)
  53. {
  54. list.add(10 - i);
  55. }
  56. list.sort();
  57. list.foreach(function(n)
  58. {
  59. println(n.value);
  60. });
  61. }
  62.  
  63. main();
  64.  
Success #stdin #stdout 0.47s 381824KB
stdin
Standard input is empty
stdout
1.0
10.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0