11package by .andd3dfx .collections .custom ;
22
33import lombok .AllArgsConstructor ;
4+ import org .apache .commons .lang3 .NotImplementedException ;
45
6+ import java .util .Collection ;
57import java .util .Iterator ;
68
79/**
810 * @see <a href="https://youtu.be/vHjvNHBQP3o">Video solution</a>
911 */
10- public class CustomLinkedList <T > implements Iterable <T > {
12+ public class CustomLinkedList <T > implements Collection <T > {
1113
1214 @ AllArgsConstructor
1315 public static class Node <M > {
@@ -26,8 +28,10 @@ public String toString() {
2628 private Node <T > head ;
2729 private int size = 0 ;
2830
29- public void add (T value ) {
31+ @ Override
32+ public boolean add (T value ) {
3033 add (size , value );
34+ return true ;
3135 }
3236
3337 public void add (int index , T value ) {
@@ -53,14 +57,37 @@ public void add(int index, T value) {
5357 size ++;
5458 }
5559
60+ @ Override
5661 public int size () {
5762 return size ;
5863 }
5964
65+ @ Override
6066 public boolean isEmpty () {
6167 return size == 0 ;
6268 }
6369
70+ @ Override
71+ public boolean contains (Object o ) {
72+ var curr = head ;
73+ if (o == null ) {
74+ while (curr != null ) {
75+ if (curr .value == null ) {
76+ return true ;
77+ }
78+ curr = curr .next ;
79+ }
80+ } else {
81+ while (curr != null ) {
82+ if (o .equals (curr .value )) {
83+ return true ;
84+ }
85+ curr = curr .next ;
86+ }
87+ }
88+ return false ;
89+ }
90+
6491 public T get (int index ) {
6592 if (index < 0 || index >= size ) {
6693 throw new IndexOutOfBoundsException (String .format ("Wrong index: %d" , index ));
@@ -120,7 +147,8 @@ public T remove(int index) {
120147 return curr .value ;
121148 }
122149
123- public boolean remove (T value ) {
150+ @ Override
151+ public boolean remove (Object value ) {
124152 var i = 0 ;
125153 var curr = head ;
126154 while (curr != null ) {
@@ -134,7 +162,35 @@ public boolean remove(T value) {
134162 return false ;
135163 }
136164
137- private boolean checkEquality (T value1 , T value2 ) {
165+ @ Override
166+ public boolean containsAll (Collection <?> c ) {
167+ for (var element : c ) {
168+ if (!contains (element )) {
169+ return false ;
170+ }
171+ }
172+ return true ;
173+ }
174+
175+ @ Override
176+ public boolean addAll (Collection <? extends T > c ) {
177+ // TODO add implementation
178+ throw new UnsupportedOperationException ();
179+ }
180+
181+ @ Override
182+ public boolean removeAll (Collection <?> c ) {
183+ // TODO add implementation
184+ throw new UnsupportedOperationException ();
185+ }
186+
187+ @ Override
188+ public boolean retainAll (Collection <?> c ) {
189+ // TODO add implementation
190+ throw new UnsupportedOperationException ();
191+ }
192+
193+ private boolean checkEquality (Object value1 , Object value2 ) {
138194 if (value1 == null ) {
139195 return value2 == null ;
140196 }
@@ -162,6 +218,7 @@ public void reverse() {
162218 head = prev ;
163219 }
164220
221+ @ Override
165222 public void clear () {
166223 head = null ;
167224 size = 0 ;
@@ -172,6 +229,18 @@ public Iterator<T> iterator() {
172229 return new CustomIterator <>(head );
173230 }
174231
232+ @ Override
233+ public Object [] toArray () {
234+ // TODO add implementation
235+ throw new NotImplementedException ();
236+ }
237+
238+ @ Override
239+ public <T1 > T1 [] toArray (T1 [] a ) {
240+ // TODO add implementation
241+ throw new NotImplementedException ();
242+ }
243+
175244 @ AllArgsConstructor
176245 public static class CustomIterator <E > implements Iterator <E > {
177246 private Node <E > curr ;
0 commit comments