Advertisement
Guest User

LinkedListImpl

a guest
Apr 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Spliterator;
  4. import java.util.function.Consumer;
  5. import java.util.Iterator;
  6.  
  7. public class LinkedListImpl<T> implements Iterable<T> {
  8. private Node<T> head;
  9. private Node<T> tail;
  10. private int count;
  11.  
  12. public LinkedListImpl() {
  13. this.head = null;
  14. this.tail = null;
  15. this.count = 0;
  16. }
  17.  
  18.  
  19. //Добавление элемента в конец списка
  20. public void add(T num) {
  21. if (!isEmpty()) {
  22. Node<T> temp = tail;
  23. tail = new Node<T>(num);
  24. temp.setNext(tail);
  25. tail.setPrevious(temp);
  26. } else {
  27. tail = new Node<T>(num);
  28. head = tail;
  29. }
  30. count++;
  31. }
  32.  
  33.  
  34. //Добавление элемента в начало списка
  35. public void addFirst(T num) {
  36. if (!isEmpty()) {
  37. Node<T> temp = head;
  38. head = new Node<T>(num);
  39. head.setNext(temp);
  40. temp.setPrevious(head);
  41. } else {
  42. tail = new Node<T>(num);
  43. head = tail;
  44. }
  45. count++;
  46. }
  47.  
  48.  
  49. //Удаление элемента по значению
  50. public boolean remove(T value) {
  51. boolean check = false;
  52. Node<T> previous = null;
  53. Node<T> current = head;
  54. while (current != null) {
  55. if (current.getValue().equals(value)) {
  56. check = true;
  57. if (count == 1) {
  58. head = null;
  59. tail = null;
  60. } else if (current.equals(head)) {
  61. head = head.getNext();
  62. head.setPrevious(null);
  63. } else if (current.equals(tail)) {
  64. tail = previous;
  65. tail.setNext(null);
  66. } else {
  67. previous.setNext(current.getNext());
  68. current.getNext().setPrevious(previous);
  69. }
  70. count--;
  71. break;
  72. }
  73. previous = current;
  74. current = previous.getNext();
  75. }
  76. if (!check) {
  77. System.out.println("Не удалось найти данный элемент");
  78. }
  79. return check;
  80. }
  81.  
  82.  
  83. //Удаление элемента по индексу
  84. public boolean removeByIndex(int index) {
  85. boolean check = false;
  86. Node<T> previous = null;
  87. Node<T> current = head;
  88. int i = 0;
  89. if (index <= count - 1) {
  90. check = true;
  91. if (count == 1) {
  92. head = null;
  93. tail = null;
  94. } else if (index == 0) {
  95. head = head.getNext();
  96. head.setPrevious(null);
  97. } else {
  98. while (i != index) {
  99. previous = current;
  100. current = current.getNext();
  101. i++;
  102. }
  103.  
  104. if (current.equals(tail)) {
  105. tail = previous;
  106. tail.setNext(null);
  107. } else {
  108. previous.setNext(current.getNext());
  109. current.getNext().setPrevious(previous);
  110. }
  111. }
  112. }
  113. if (!check) {
  114. System.out.println("Не удалось найти элемент с данным индексом");
  115. }
  116. return check;
  117.  
  118. }
  119.  
  120.  
  121. //Вывод списка
  122. public void print() {
  123. Node<T> current = head;
  124. while (current.getNext() != null) {
  125. System.out.print(current.getValue() + "->");
  126. current = current.getNext();
  127. }
  128. System.out.println(current.getValue());
  129. }
  130.  
  131.  
  132. //Получение элемента по индексу
  133. public T get(int index) {
  134. if (index > count - -1) {
  135. System.out.println("Элемент с данным индексом не найден");
  136. return null;
  137. } else {
  138. Node<T> current = head;
  139. int i = 0;
  140. while (i != index) {
  141. current = current.getNext();
  142. i++;
  143. }
  144. return (T) current.getValue();
  145. }
  146.  
  147. }
  148.  
  149. //Удаление всех элементов из списка
  150. public void clear() {
  151. head = null;
  152. tail = null;
  153. count = 0;
  154. }
  155.  
  156.  
  157. //Проверка наличия элементов
  158. public boolean isEmpty() {
  159. return count == 0;
  160. }
  161.  
  162. @Override
  163. public Iterator iterator() {
  164. return new LinkedListIterator();
  165. }
  166.  
  167. @Override
  168. public void forEach(Consumer action) {
  169. Node current = head;
  170. while (current.getNext() != null) {
  171. action.accept(current.getValue());
  172. current = current.getNext();
  173. }
  174. action.accept(current.getValue());
  175. }
  176.  
  177. @Override
  178. public Spliterator spliterator() {
  179. return null;
  180. }
  181.  
  182.  
  183. private class LinkedListIterator implements Iterator<T> {
  184.  
  185. private Node<T> currentNode;
  186. private int currentIndex;
  187.  
  188. public LinkedListIterator() {
  189. this.currentIndex = 0;
  190. this.currentNode = new Node<T>(null);
  191. this.currentNode.setNext(head);
  192. }
  193.  
  194.  
  195. @Override
  196. public boolean hasNext() {
  197. return currentIndex < count ;
  198. }
  199.  
  200. @Override
  201. public T next() {
  202. currentNode = currentNode.getNext();
  203. currentIndex++;
  204. return currentNode.value;
  205. }
  206.  
  207.  
  208. }
  209.  
  210.  
  211. public MyStream<T> stream() {
  212. return new MyStream<T>(this);
  213. }
  214.  
  215. public static class Node<E> {
  216. private E value;
  217. private Node<E> next;
  218. private Node<E> previous;
  219.  
  220. public Node(E value) {
  221. this.value = value;
  222. }
  223.  
  224. public E getValue() {
  225. return value;
  226. }
  227.  
  228. public void setValue(E value) {
  229. this.value = value;
  230. }
  231.  
  232. public Node getNext() {
  233. return next;
  234. }
  235.  
  236. public void setNext(Node next) {
  237. this.next = next;
  238. }
  239.  
  240. public void setPrevious(Node previous) {
  241. this.previous = previous;
  242. }
  243.  
  244. public Node getPrevious() {
  245. return previous;
  246. }
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement