Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. public class DoubleLinkedList {
  2.  
  3. private Node head;
  4.  
  5. //TODO: addLast() recursivo
  6.  
  7. public void addLast(int value) {
  8. if(this.head == null) {
  9. this.head = new Node(value);
  10. }else {
  11. addLast(this.head, value);
  12. }
  13. }
  14.  
  15. private void addLast(Node node, int value) {
  16. if(node.next == null) {
  17. node.next = new Node(value);
  18. node.next.previous = node;
  19. }else {
  20. addLast(node.next, value);
  21. }
  22. }
  23.  
  24. //TODO: size() recursivo
  25.  
  26. public int size() {
  27. return size(this.head);
  28. }
  29.  
  30. private int size(Node node) {
  31. if(node == null) {
  32. return 0;
  33. }else {
  34. return 1 + size(node.next);
  35. }
  36. }
  37.  
  38. //TODO: soma() recursivo
  39.  
  40. public int soma() {
  41. return soma(this.head);
  42. }
  43.  
  44. private int soma(Node node) {
  45. if(node == null) {
  46. return 0;
  47. }else {
  48. return node.value + soma(node.next);
  49. }
  50. }
  51.  
  52. //TODO: isSorted() recursivo
  53.  
  54. public boolean isSorted() {
  55. return isSorted(this.head);
  56. }
  57.  
  58. private boolean isSorted(Node node) {
  59. if(node == null) {
  60. return true;
  61. }
  62. if(node.next == null) {
  63. return true;
  64. }else if(node.value <= node.next.value) {
  65. return isSorted(node.next);
  66. }
  67. return false;
  68. }
  69.  
  70. //TODO: add() iterativo (Adiciona naquele índice e o que estava vai para o próximo)
  71.  
  72. public void add(int index, int value) {
  73. if(index < size() && index >= 0) {
  74.  
  75. Node newNode = new Node(value);
  76. Node aux = this.head;
  77.  
  78. if(index == 0) {
  79. newNode.next = aux;
  80. aux.previous = newNode;
  81. this.head = newNode;
  82. return;
  83. }
  84.  
  85. int counter = index;
  86. while(counter > 0) {
  87. aux = aux.next;
  88.  
  89. counter--;
  90. }
  91. newNode.next = aux;
  92. newNode.previous = aux.previous;
  93.  
  94. aux.previous.next = newNode;
  95. aux.previous = newNode;
  96. }
  97. }
  98.  
  99. //TODO: remove() iterativo
  100.  
  101. public void remove(int index) {
  102. if(index < size()) {
  103.  
  104. int counter = index;
  105. Node aux = this.head;
  106.  
  107. while(counter > 0) {
  108. aux = aux.next;
  109.  
  110. counter--;
  111. }
  112.  
  113. if(aux != this.head) {
  114. aux.previous.next = aux.next;
  115.  
  116. if(aux.next != null) {
  117. aux.next = aux.previous;
  118. }
  119. }else {
  120. if(this.head != null) {
  121. this.head = this.head.next;
  122. if(this.head != null) {
  123. this.head.previous = null;
  124. }
  125. }
  126. }
  127.  
  128. }
  129. }
  130.  
  131. public String toString() {
  132. String str = "";
  133.  
  134. Node aux = this.head;
  135. while(aux != null) {
  136. str += aux.value + ", ";
  137. aux = aux.next;
  138. }
  139. if(str.length()>=2) {
  140. return "[" + str.substring(0, str.length()-2) + "]";
  141. }
  142. return "[" + str + "]";
  143. }
  144. }
  145.  
  146. class Node {
  147.  
  148. int value;
  149. Node previous;
  150. Node next;
  151.  
  152. public Node(int value) {
  153. this.value = value;
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement