Skip to content

Commit 12e944f

Browse files
committed
Merge branch 'main' into exercise/completed
2 parents 0397a73 + d046540 commit 12e944f

File tree

3 files changed

+114
-58
lines changed

3 files changed

+114
-58
lines changed

2-0-data-structures-and-algorithms/src/main/java/com/bobocode/linked_list/LinkedList.java

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.bobocode.linked_list;
22

3+
import java.util.NoSuchElementException;
4+
35
/**
46
* {@link LinkedList} is a list implementation that is based on singly linked generic nodes. A node is implemented as
57
* inner static class {@link Node<T>}. In order to keep track on nodes, {@link LinkedList} keeps a reference to a head node.
@@ -65,6 +67,28 @@ public T get(int index) {
6567
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
6668
}
6769

70+
/**
71+
* Returns the first element of the list. Operation is performed in constant time O(1)
72+
*
73+
* @return the first element of the list
74+
* @throws java.util.NoSuchElementException if list is empty
75+
*/
76+
@Override
77+
public T getFirst() {
78+
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
79+
}
80+
81+
/**
82+
* Returns the last element of the list. Operation is performed in constant time O(1)
83+
*
84+
* @return the last element of the list
85+
* @throws java.util.NoSuchElementException if list is empty
86+
*/
87+
@Override
88+
public T getLast() {
89+
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
90+
}
91+
6892
/**
6993
* Removes an elements by its position index. In case provided index in out of the list bounds it
7094
* throws {@link IndexOutOfBoundsException}

2-0-data-structures-and-algorithms/src/main/java/com/bobocode/linked_list/List.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package com.bobocode.linked_list;
22

33

4-
public interface List<T> {
5-
void add(T element);
4+
public interface List<E> {
5+
void add(E element);
66

7-
void add(int index, T element);
7+
void add(int index, E element);
88

9-
void set(int index, T element);
9+
void set(int index, E element);
1010

11-
T get(int index);
11+
E get(int index);
12+
13+
E getFirst();
14+
15+
E getLast();
1216

1317
void remove(int index);
1418

15-
boolean contains(T element);
19+
boolean contains(E element);
1620

1721
boolean isEmpty();
1822

2-0-data-structures-and-algorithms/src/test/java/com/bobocode/linked_list/LinkedListTest.java

+80-52
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.junit.jupiter.api.Test;
77
import org.junit.jupiter.api.TestMethodOrder;
88

9+
import java.util.NoSuchElementException;
10+
911
import static org.junit.jupiter.api.Assertions.assertEquals;
1012
import static org.junit.jupiter.api.Assertions.assertFalse;
1113
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -36,7 +38,7 @@ void testGetFirstElementFromSingleElementList() {
3638
}
3739

3840
@Test
39-
@Order(4)
41+
@Order(3)
4042
void testAddElements() {
4143
intList = LinkedList.of(43, 233, 54);
4244

@@ -46,42 +48,59 @@ void testAddElements() {
4648
assertEquals(54, intList.get(2).intValue());
4749
}
4850

51+
@Test
52+
@Order(4)
53+
void testSize() {
54+
intList = LinkedList.of(4, 7, 9, 0, 7);
55+
56+
int size = intList.size();
57+
58+
assertEquals(5, size);
59+
}
4960

5061
@Test
5162
@Order(5)
52-
void testGetElements() {
53-
intList = LinkedList.of(25, 87, 45);
54-
55-
int firstElement = intList.get(0);
56-
int secondElement = intList.get(1);
57-
int thirdElement = intList.get(2);
63+
void testGetFirstElement() {
64+
intList = LinkedList.of(31, 32);
5865

59-
assertEquals(25, firstElement);
60-
assertEquals(87, secondElement);
61-
assertEquals(45, thirdElement);
66+
assertEquals(31, intList.getFirst().intValue());
6267
}
6368

6469
@Test
65-
@Order(16)
66-
void testGetFirstElementFromEmptyList() {
67-
assertThrows(IndexOutOfBoundsException.class, () -> intList.get(0));
70+
@Order(6)
71+
void testGetLastElement() {
72+
intList = LinkedList.of(41, 42);
73+
74+
assertEquals(42, intList.getLast().intValue());
6875
}
6976

7077
@Test
71-
@Order(17)
72-
void testGetElementByNegativeIndex() {
73-
assertThrows(IndexOutOfBoundsException.class, () -> intList.get(-1));
78+
@Order(7)
79+
void testGetFirstOfEmptyList() { assertThrows(NoSuchElementException.class, () -> intList.getFirst()); }
80+
81+
@Test
82+
@Order(8)
83+
void testGetLastOfEmptyList() {
84+
assertThrows(NoSuchElementException.class, () -> intList.getLast());
7485
}
7586

87+
7688
@Test
77-
@Order(18)
78-
void testGetElementByIndexEqualsToListSize() {
79-
intList = LinkedList.of(33, 46, 25, 87, 45);
80-
assertThrows(IndexOutOfBoundsException.class, () -> intList.get(5));
89+
@Order(9)
90+
void testGetElements() {
91+
intList = LinkedList.of(25, 87, 45);
92+
93+
int firstElement = intList.get(0);
94+
int secondElement = intList.get(1);
95+
int thirdElement = intList.get(2);
96+
97+
assertEquals(25, firstElement);
98+
assertEquals(87, secondElement);
99+
assertEquals(45, thirdElement);
81100
}
82101

83102
@Test
84-
@Order(6)
103+
@Order(10)
85104
void testAddElementByZeroIndexIntoEmptyList() {
86105
intList.add(0, 45);
87106

@@ -90,7 +109,7 @@ void testAddElementByZeroIndexIntoEmptyList() {
90109
}
91110

92111
@Test
93-
@Order(7)
112+
@Order(11)
94113
void testAddElementByIndexToTheEndOfList() {
95114
intList = LinkedList.of(98, 64, 23, 1, 3, 4);
96115

@@ -102,7 +121,7 @@ void testAddElementByIndexToTheEndOfList() {
102121
}
103122

104123
@Test
105-
@Order(8)
124+
@Order(12)
106125
void testAddElementToTheHeadOfNonEmptyList() {
107126
intList = LinkedList.of(4, 6, 8, 9, 0, 2);
108127

@@ -114,7 +133,7 @@ void testAddElementToTheHeadOfNonEmptyList() {
114133
}
115134

116135
@Test
117-
@Order(9)
136+
@Order(13)
118137
void testAddElementByIndex() {
119138
intList = LinkedList.of(43, 5, 6, 8);
120139

@@ -130,14 +149,14 @@ void testAddElementByIndex() {
130149
}
131150

132151
@Test
133-
@Order(10)
152+
@Order(14)
134153
void testAddElementByNegativeIndex() {
135154
assertThrows(IndexOutOfBoundsException.class, () -> intList.add(-1, 66));
136155

137156
}
138157

139158
@Test
140-
@Order(11)
159+
@Order(15)
141160
void testAddElementByIndexLargerThanListSize() {
142161
intList = LinkedList.of(4, 6, 11, 9);
143162

@@ -146,7 +165,7 @@ void testAddElementByIndexLargerThanListSize() {
146165
}
147166

148167
@Test
149-
@Order(12)
168+
@Order(16)
150169
void testAddElementByIndexEqualToSize() {
151170
intList = LinkedList.of(1, 2, 3, 4, 5); // size = 5
152171

@@ -157,21 +176,21 @@ void testAddElementByIndexEqualToSize() {
157176
}
158177

159178
@Test
160-
@Order(13)
179+
@Order(17)
161180
void testSetFirstElementOnEmptyTree() {
162181
assertThrows(IndexOutOfBoundsException.class, () -> intList.set(0, 34));
163182
}
164183

165184
@Test
166-
@Order(14)
185+
@Order(18)
167186
void testSetElementByIndexEqualToSize() {
168187
intList = LinkedList.of(2, 3, 4); // size = 3
169188

170189
assertThrows(IndexOutOfBoundsException.class, () -> intList.set(3, 222));
171190
}
172191

173192
@Test
174-
@Order(15)
193+
@Order(19)
175194
void testSetElementByIndex() {
176195
intList = LinkedList.of(34, 78, 9, 8);
177196

@@ -187,13 +206,32 @@ void testSetElementByIndex() {
187206
}
188207

189208
@Test
190-
@Order(19)
209+
@Order(20)
210+
void testGetFirstElementFromEmptyList() {
211+
assertThrows(IndexOutOfBoundsException.class, () -> intList.get(0));
212+
}
213+
214+
@Test
215+
@Order(21)
216+
void testGetElementByNegativeIndex() {
217+
assertThrows(IndexOutOfBoundsException.class, () -> intList.get(-1));
218+
}
219+
220+
@Test
221+
@Order(22)
222+
void testGetElementByIndexEqualsToListSize() {
223+
intList = LinkedList.of(33, 46, 25, 87, 45);
224+
assertThrows(IndexOutOfBoundsException.class, () -> intList.get(5));
225+
}
226+
227+
@Test
228+
@Order(23)
191229
void testRemoveElementFromEmptyList() {
192230
assertThrows(IndexOutOfBoundsException.class, () -> intList.remove(234));
193231
}
194232

195233
@Test
196-
@Order(20)
234+
@Order(24)
197235
void testRemoveFirstElement() {
198236
intList = LinkedList.of(4, 6, 8, 9);
199237

@@ -204,7 +242,7 @@ void testRemoveFirstElement() {
204242
}
205243

206244
@Test
207-
@Order(21)
245+
@Order(25)
208246
void testRemoveLastElement() {
209247
intList = LinkedList.of(4, 6, 8, 9);
210248

@@ -215,7 +253,7 @@ void testRemoveLastElement() {
215253
}
216254

217255
@Test
218-
@Order(22)
256+
@Order(26)
219257
void testRemoveElement() {
220258
intList = LinkedList.of(1, 2, 3, 4, 5);
221259

@@ -227,15 +265,15 @@ void testRemoveElement() {
227265
}
228266

229267
@Test
230-
@Order(23)
268+
@Order(27)
231269
void testContainsOnEmptyList() {
232270
boolean contains = intList.contains(34);
233271

234272
assertFalse(contains);
235273
}
236274

237275
@Test
238-
@Order(24)
276+
@Order(28)
239277
void testContains() {
240278
intList = LinkedList.of(45, 6, 3, 6);
241279

@@ -247,15 +285,15 @@ void testContains() {
247285
}
248286

249287
@Test
250-
@Order(25)
288+
@Order(29)
251289
void testIsEmptyOnEmptyList() {
252290
boolean empty = intList.isEmpty();
253291

254292
assertTrue(empty);
255293
}
256294

257295
@Test
258-
@Order(26)
296+
@Order(30)
259297
void testIsEmpty() {
260298
intList = LinkedList.of(34, 5, 6);
261299

@@ -265,33 +303,23 @@ void testIsEmpty() {
265303
}
266304

267305
@Test
268-
@Order(27)
306+
@Order(31)
269307
void testSizeOnEmptyList() {
270308
int size = intList.size();
271309

272310
assertEquals(0, size);
273311
}
274312

275313
@Test
276-
@Order(3)
277-
void testSize() {
278-
intList = LinkedList.of(4, 7, 9, 0, 7);
279-
280-
int size = intList.size();
281-
282-
assertEquals(5, size);
283-
}
284-
285-
@Test
286-
@Order(28)
314+
@Order(32)
287315
void testClearOnEmptyList() {
288316
intList.clear();
289317

290318
assertEquals(0, intList.size());
291319
}
292320

293321
@Test
294-
@Order(29)
322+
@Order(33)
295323
void testClearChangesTheSize() {
296324
intList = LinkedList.of(4, 5, 6);
297325

@@ -301,7 +329,7 @@ void testClearChangesTheSize() {
301329
}
302330

303331
@Test
304-
@Order(30)
332+
@Order(34)
305333
void testClearRemovesElements() {
306334
intList = LinkedList.of(4, 5, 6);
307335

0 commit comments

Comments
 (0)