Skip to content

Commit b03f4f1

Browse files
committed
Merge pull request #99 from exercism/new-exercise-simple-linked-list
Add new exercise simple linked list
2 parents 2becaf4 + c4b10d7 commit b03f4f1

File tree

7 files changed

+173
-0
lines changed

7 files changed

+173
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ build/
99
bin/configlet
1010
bin/configlet.exe
1111
gradle/
12+
/exercises/.nb-gradle/

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"octal",
3939
"luhn",
4040
"pig-latin",
41+
"simple-linked-list",
4142
"linked-list",
4243
"nth-prime",
4344
"pascals-triangle"

exercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include 'prime-factors'
3030
include 'scrabble-score'
3131
include 'sieve'
3232
include 'simple-cipher'
33+
include 'simple-linked-list'
3334
include 'space-age'
3435
include 'strain'
3536
include 'triangle'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.10"
11+
testCompile "org.assertj:assertj-core:3.2.0"
12+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
import java.lang.reflect.Array;
3+
import java.util.NoSuchElementException;
4+
import java.util.Objects;
5+
6+
public class SimpleLinkedList<T> {
7+
8+
private static class Element<T> {
9+
10+
final T value;
11+
Element next;
12+
13+
Element(T value) {
14+
this.value = value;
15+
}
16+
}
17+
18+
private Element<T> head;
19+
private int size;
20+
21+
public SimpleLinkedList() {
22+
}
23+
24+
public SimpleLinkedList(T[] values) {
25+
for (int ii = values.length - 1; ii >= 0; ii--) {
26+
push(values[ii]);
27+
}
28+
}
29+
30+
public final void push(T value) {
31+
Element<T> newElement = new Element<>(value);
32+
this.size++;
33+
if (Objects.isNull(head)) {
34+
head = newElement;
35+
} else {
36+
newElement.next = head;
37+
head = newElement;
38+
}
39+
}
40+
41+
public T pop() {
42+
if (Objects.isNull(head)) {
43+
throw new NoSuchElementException();
44+
}
45+
T value = head.value;
46+
head = head.next;
47+
this.size--;
48+
return value;
49+
}
50+
51+
public void reverse() {
52+
Element<T> current = head;
53+
Element<T> next;
54+
Element<T> previous = null;
55+
while (Objects.nonNull(current)) {
56+
next = current.next;
57+
current.next = previous;
58+
previous = current;
59+
current = next;
60+
}
61+
head = previous;
62+
}
63+
64+
public T[] asArray(Class<T> clazz) {
65+
T[] result = newArray(clazz, this.size);
66+
int index = 0;
67+
Element<T> current = head;
68+
while (Objects.nonNull(current)) {
69+
result[index++] = current.value;
70+
current = current.next;
71+
}
72+
return result;
73+
}
74+
75+
private <T> T[] newArray(Class<T> clazz, int size) {
76+
@SuppressWarnings("unchecked")
77+
T[] arr = (T[]) Array.newInstance(clazz, size);
78+
79+
return arr;
80+
}
81+
82+
public int size() {
83+
return this.size;
84+
}
85+
}

exercises/simple-linked-list/src/main/java/.keep

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
import java.util.NoSuchElementException;
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import org.junit.Test;
5+
6+
public class SimpleLinkedListTest {
7+
8+
@Test
9+
public void aNewListIsEmpty() {
10+
SimpleLinkedList list = new SimpleLinkedList();
11+
assertThat(list.size()).isEqualTo(0);
12+
}
13+
14+
@Test
15+
public void canCreateFromArray() {
16+
Integer[] values = new Integer[]{1, 2, 3};
17+
SimpleLinkedList list = new SimpleLinkedList(values);
18+
assertThat(list.size()).isEqualTo(3);
19+
}
20+
21+
@Test(expected = NoSuchElementException.class)
22+
public void popOnEmptyListWillThrow() {
23+
SimpleLinkedList list = new SimpleLinkedList();
24+
list.pop();
25+
}
26+
27+
@Test
28+
public void popReturnsLastAddedElement() {
29+
SimpleLinkedList list = new SimpleLinkedList();
30+
list.push(9);
31+
list.push(8);
32+
assertThat(list.size()).isEqualTo(2);
33+
assertThat(list.pop()).isEqualTo(8);
34+
assertThat(list.pop()).isEqualTo(9);
35+
assertThat(list.size()).isEqualTo(0);
36+
}
37+
38+
@Test
39+
public void reverseReversesList() {
40+
SimpleLinkedList list = new SimpleLinkedList();
41+
list.push(9);
42+
list.push(8);
43+
list.push(7);
44+
list.push(6);
45+
list.push(5);
46+
list.reverse();
47+
assertThat(list.pop()).isEqualTo(9);
48+
assertThat(list.pop()).isEqualTo(8);
49+
assertThat(list.pop()).isEqualTo(7);
50+
assertThat(list.pop()).isEqualTo(6);
51+
assertThat(list.pop()).isEqualTo(5);
52+
}
53+
54+
@Test
55+
public void canReturnListAsArray() {
56+
SimpleLinkedList list = new SimpleLinkedList();
57+
list.push(9);
58+
list.push(8);
59+
list.push(7);
60+
list.push(6);
61+
list.push(5);
62+
Integer[] expected = {5, 6, 7, 8, 9};
63+
assertThat(list.asArray(Integer.class)).containsExactly(expected);
64+
}
65+
66+
@Test
67+
public void canReturnEmptyListAsEmptyArray() {
68+
SimpleLinkedList list = new SimpleLinkedList();
69+
Object[] expected = {};
70+
assertThat(list.asArray(Object.class)).containsExactly(expected);
71+
}
72+
73+
}

0 commit comments

Comments
 (0)