Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. public class LinkedList {
  2. Item head;
  3. Item tail;
  4.  
  5. public LinkedList() {
  6. head = null;
  7. tail = null;
  8. }
  9.  
  10. // Add an item to the end of the list
  11. public void add(Item x) {
  12. if (tail == null) {
  13. tail = x;
  14. head = x;
  15. }
  16. else {
  17. tail.next = x;
  18. x.previous = tail;
  19. tail = x;
  20. }
  21. }
  22.  
  23. // Remove the given item from the list
  24. public void remove(Item x) {
  25. if (x == head) {
  26. if (x == tail) {
  27. head = tail = null;
  28. }
  29. else {
  30. head = x.next;
  31. head.previous = null;
  32. }
  33. }
  34. else {
  35. if (x == tail) {
  36. tail = x.previous;
  37. tail.next = null;
  38. }
  39. else {
  40. x.previous.next = x.next;
  41. x.next.previous = x.previous;
  42. }
  43. }
  44. }
  45.  
  46. // Return a string representation of the list
  47. public String toString() {
  48. if (head == null)
  49. return "[EMPTY]";
  50.  
  51. String s = "[H:";
  52. Item currentItem = head;
  53. while (currentItem != null) {
  54. s += currentItem.data;
  55. if (currentItem != tail)
  56. s += "]<==>[";
  57. currentItem = currentItem.next;
  58. }
  59. return s + ":T]";
  60. }
  61.  
  62. // Add up the total data in the list
  63. public int totalData() {
  64. if (head == null)
  65. return 0;
  66.  
  67. int total = 0;
  68. Item currentItem = head;
  69. while (currentItem != null) {
  70. total += currentItem.data;
  71. currentItem = currentItem.next;
  72. }
  73. return total;
  74. }
  75.  
  76. // Add up the total data in the list using recursion
  77. public int totalDataRecursive() {
  78. return totalDataRecursive(head);
  79. }
  80.  
  81. // Add up the total data in the list using recursion
  82. private int totalDataRecursive(Item start) {
  83. if (start == null)
  84. return 0;
  85. return start.data + totalDataRecursive(start.next);
  86. }
  87.  
  88. // Return a new linked list containing all items with odd data from this list using recursion
  89. public LinkedList oddItems() {
  90. return oddItems(head);
  91. }
  92.  
  93. // Return all items with odd data in the list using recursion
  94. private LinkedList oddItems(Item start) {
  95. if (start == null)
  96. return new LinkedList();
  97.  
  98. LinkedList result = oddItems(start.next);
  99.  
  100. if (start.data %2 != 0)
  101. result.add(new Item(start.data));
  102.  
  103. return result;
  104. }
  105.  
  106. // Return all items with odd data in the list using recursion
  107. private LinkedList oddItems2(Item startItem, LinkedList resultList) {
  108. if (startItem == null)
  109. return resultList;
  110.  
  111. if (startItem.data %2 != 0)
  112. resultList.add(new Item(startItem.data));
  113.  
  114. return oddItems2(startItem.next, resultList);
  115. }
  116.  
  117. // Return a new linked list containing all common elements of the two lists
  118. public LinkedList inCommon(LinkedList aList) {
  119. return inCommon(this.head, aList.head, new LinkedList());
  120. }
  121.  
  122. // Return all items which are common between the two lists
  123. private LinkedList inCommon(Item start1, Item start2, LinkedList result) {
  124. if ((start1 == null) || (start2 == null))
  125. return result;
  126.  
  127. if (contains(start1,start2.data))
  128. result.add(new Item(start2.data));
  129.  
  130. return inCommon(start1, start2.next, result);
  131. }
  132.  
  133. // Return a boolean indicating whether or not the list contains a given item's data
  134. public boolean contains(Item startItem, byte data) {
  135. if (startItem == null)
  136. return false;
  137. if (startItem.data == data)
  138. return true;
  139. else
  140. return contains(startItem.next, data);
  141. }
  142.  
  143. public boolean isInIncreasingOrder(Item it) {
  144. boolean sorted = true;
  145. for(int i = 1; i < list.size(); i++) {
  146. if(list.get(i-1).compareTo(list.get(i)) > 0){
  147. sorted = false;
  148. }
  149. }
  150. return sorted;
  151.  
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement