Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2.  
  3. public class MyCircularLinkedList<E> implements MyList<E> {
  4. private Node<E> tail;
  5. private int size = 0;
  6.  
  7.  
  8. public MyCircularLinkedList() {
  9. }
  10.  
  11.  
  12.  
  13. public MyCircularLinkedList(E[] objects) {
  14. for (int i = 0; i < objects.length; i++)
  15. add(objects[i]);
  16. }
  17.  
  18.  
  19.  
  20. public E getFirst() {
  21. if (size == 0) {
  22. throw new NoSuchElementException();
  23. }
  24. else {
  25. return tail.next.element;
  26. }
  27. }
  28.  
  29.  
  30. public E getLast() {
  31. if (size == 0) {
  32. throw new NoSuchElementException();
  33. }
  34. else {
  35. return tail.element;
  36. }
  37. }
  38.  
  39. public void addFirst(E e) {
  40. Node<E> newNode = new Node<>(e);
  41. if(size == 0) {
  42. newNode.next = newNode;
  43. tail = newNode;
  44. newNode.next = tail.next;
  45. }
  46. else{
  47. newNode.next=tail.next;
  48. tail.next=newNode;
  49. }
  50.  
  51.  
  52. size++;
  53.  
  54.  
  55. }
  56.  
  57.  
  58. public void addLast(E e) {
  59. Node<E> newNode = new Node<>(e);
  60.  
  61. if (tail == null) {
  62. tail.next = tail = newNode;
  63. }
  64. else {
  65. newNode.next = tail.next;
  66. tail.next = newNode;
  67. tail = newNode;
  68. }
  69.  
  70. size++;
  71. }
  72.  
  73. @Override
  74. public void add(int index, E e) {
  75. if (index > size || index < 0) throw new IndexOutOfBoundsException();
  76. if (index == 0) {
  77. addFirst(e);
  78. }
  79. else if (index >= size) {
  80. addLast(e);
  81. }
  82. else {
  83. Node<E> current = tail.next;
  84. for (int i = 1; i < index; i++) {
  85. current = current.next;
  86. }
  87. Node<E> temp = current.next;
  88. current.next = new Node<>(e);
  89. (current.next).next = temp;
  90. size++;
  91. }
  92. }
  93.  
  94.  
  95. public E removeFirst() {
  96. if (size == 0) {
  97. throw new NoSuchElementException();
  98. }
  99. else {
  100. E temp = tail.next.element;
  101. tail.next = (tail.next).next;
  102. size--;
  103. if (tail.next == null) {
  104. tail = null;
  105. }
  106. return temp;
  107. }
  108. }
  109.  
  110.  
  111. public E removeLast() {
  112. if (size == 0) {
  113. throw new NoSuchElementException();
  114. }
  115. else if (size == 1) {
  116. E temp = tail.next.element;
  117. tail.next = tail = null;
  118. size = 0;
  119. return temp;
  120. }
  121. else {
  122. Node<E> current = tail.next;
  123.  
  124. for (int i = 0; i < size - 2; i++) {
  125. current = current.next;
  126. }
  127. current.next = tail.next;
  128. E temp = tail.element;
  129.  
  130. tail = current;
  131.  
  132.  
  133.  
  134. size--;
  135. return temp;
  136. }
  137. }
  138.  
  139. @Override
  140. public E remove(int index) {
  141. if (index < 0 || index >= size) {
  142. return null;
  143. }
  144. else if (index == 0) {
  145. return removeFirst();
  146. }
  147. else if (index == size - 1) {
  148. return removeLast();
  149. }
  150. else {
  151. Node<E> previous = tail.next;
  152.  
  153. for (int i = 1; i < index; i++) {
  154. previous = previous.next;
  155. }
  156.  
  157. Node<E> current = previous.next;
  158. previous.next = current.next;
  159. size--;
  160. return current.element;
  161. }
  162. }
  163.  
  164. @Override
  165. public String toString() {
  166. StringBuilder result = new StringBuilder("[");
  167. if(tail!=null){
  168. Node<E> current = tail.next;
  169.  
  170. for (int i = 0; i < size; i++) {
  171. result.append(current.element);
  172. current = current.next;
  173. if (i != size - 1) {
  174. result.append(", ");
  175. }
  176. }
  177. }
  178. result.append("]");
  179. return result.toString();
  180. }
  181.  
  182. @Override
  183. public void clear() {
  184. size = 0;
  185. tail.next = tail = null;
  186. }
  187.  
  188. @Override
  189. public boolean contains(Object e) {
  190. if (size == 0) { return false; }
  191. Node<E> current = tail.next;
  192. for (int i = 0; i < size; i++) {
  193. if (e.equals(current.element)) {
  194. return true;
  195. }
  196. current = current.next;
  197. }
  198. return false;
  199. }
  200.  
  201. @Override
  202. public E get(int index) {
  203. if (index < 0 || index >= size) {
  204. throw new IndexOutOfBoundsException(index + "");
  205. }
  206. Node<E> current = tail.next;
  207. for (int i = 0; i < index; i++) {
  208. current = current.next;
  209. }
  210. return current.element;
  211. }
  212.  
  213. @Override
  214. public int indexOf(Object e) {
  215. if (size == 0) { return -1; }
  216. Node<E> current = tail.next;
  217. for (int i = 0; i < size; i++) {
  218. if (e.equals(current.element)) { return i; }
  219. current = current.next;
  220. }
  221. return -1;
  222. }
  223.  
  224. @Override
  225. public int lastIndexOf(E e) {
  226. if (size == 0) { return -1; }
  227. Node<E> current = tail.next;
  228. int lastIndex = -1;
  229. for (int i = 0; i < size; i++) {
  230. if (e.equals(current.element)) { lastIndex = i; }
  231. current = current.next;
  232. }
  233. return lastIndex;
  234. }
  235.  
  236. @Override
  237. public E set(int index, E e) {
  238. if (index < 0 || index >= size) {
  239. throw new IndexOutOfBoundsException(index + "");
  240. }
  241. Node<E> current = tail.next;
  242. for (int i = 0; i < index; i++) {
  243. current = current.next;
  244. }
  245. E temp = current.element;
  246. current.element = e;
  247. return temp;
  248. }
  249.  
  250. @Override
  251. public java.util.Iterator<E> iterator() {
  252. return new CircularLinkedListIterator();
  253. }
  254.  
  255. private class CircularLinkedListIterator implements java.util.Iterator<E> {
  256. private Node<E> current = tail.next;
  257. private Node<E> temp = tail;
  258. @Override
  259. public E next() {
  260.  
  261. E e = current.element;
  262. if(current.element == tail.element){
  263. current = null;
  264. }
  265. else {
  266. current = current.next;
  267. }
  268. return e;
  269. }
  270. @Override
  271. public boolean hasNext() {
  272.  
  273. return (current!=null);
  274.  
  275. }
  276. @Override
  277. public void remove() {
  278. throw new UnsupportedOperationException();
  279. }
  280. }
  281.  
  282. private static class Node<E> {
  283. E element;
  284. Node<E> next;
  285.  
  286. public Node(E element) {
  287. this.element = element;
  288. }
  289. }
  290.  
  291. @Override
  292. public int size() {
  293. return size;
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement