Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. public class SortedList {
  2. public Node first;
  3. public SortedList() {
  4. }
  5.  
  6.  
  7. public SortedList cons(int n) {
  8. Node node = this.first;
  9. if(this.first == null) {
  10. this.first = new Node(n, null);
  11. return this;
  12. }
  13. while(node.next != null) {
  14. node = node.next;
  15. }
  16. Node a = new Node(n, null);
  17. node.setNext(a);
  18. this.Sort();
  19. System.out.println("Sorted call");
  20. return this;
  21. }
  22.  
  23. public int head() {
  24. return first.next.value;
  25. }
  26.  
  27. public SortedList tail() {
  28. SortedList l = new SortedList();
  29. Node node = this.first;
  30. while(node.next != null) {
  31. node = node.next;
  32. }
  33. l.cons(node.value);
  34. return l;
  35. }
  36.  
  37. public boolean isNil() {
  38. if(this.first == null)
  39. return true;
  40. else
  41. return false;
  42. }
  43.  
  44. public int length() {
  45. Node node = this.first;
  46. int i = 0;
  47. while (node.next != null) {
  48. node = node.next;
  49. ++i;
  50. }
  51. return i;
  52. }
  53.  
  54. public SortedList append(SortedList l) {
  55. Node node = this.first;
  56. while(node.next != null) {
  57. node = node.next;
  58. }
  59. Node node2 = l.first;
  60. while(node2.next != null) {
  61. this.cons(node2.next.value);
  62. node2 = node2.next;
  63. }
  64. this.Sort();
  65. return this;
  66. }
  67.  
  68. public int last() {
  69. Node node = this.first;
  70. while(node.next != null) {
  71. node = node.next;
  72. }
  73. return node.value;
  74. }
  75.  
  76. public SortedList init() {
  77. SortedList l = new SortedList();
  78. Node node = l.first;
  79. while(node.next != null) {
  80. node = node.next;
  81. l.cons(node.value);
  82. }
  83. return l;
  84. }
  85.  
  86. public void Sort() {
  87. int temp;
  88. for(int i=1; i<this.length(); i++) {
  89. for(int j=0; j<this.length()-i; j++) {
  90. if(this.getbyIndexVal(j)>this.getbyIndexVal(j+1)) {
  91. System.out.println("Switched Values");
  92. temp=this.getbyIndexVal(j);
  93. this.getbyIndexNode(j).setValue(this.getbyIndexVal(j+1));
  94. this.getbyIndexNode(j+1).setValue(temp);
  95. }
  96.  
  97. }
  98. }
  99.  
  100. }
  101.  
  102. public int getbyIndexVal(int i) {
  103. if(i >= this.length()) return 0;
  104. int j = 0;
  105. Node node = this.first;
  106. while(i != j) {
  107. node = node.next;
  108. j++;
  109. }
  110. return node.value;
  111. }
  112. public Node getbyIndexNode(int i) {
  113. if(i >= this.length()) return first;
  114. int j = 0;
  115. Node node = this.first;
  116. while(i != j) {
  117. node = node.next;
  118. j++;
  119. }
  120. return node;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement