linked list example (head only)

This commit is contained in:
Wouter Groeneveld 2018-03-26 17:35:42 +02:00
parent c4b579b463
commit 8bcbd6c823
4 changed files with 152 additions and 8 deletions

View File

@ -0,0 +1,60 @@
package be.brainbaking.lists;
import java.util.ArrayList;
import java.util.List;
public class SomeLinkedList<T> {
private SomeLinkedObject<T> head;
public SomeLinkedList() {
}
public SomeLinkedList(T singleItem) {
add(singleItem);
}
public List<T> toList() {
List<T> theList = new ArrayList<>();
SomeLinkedObject<T> curr = head;
for(int i = 0; i < size(); i++) {
if(curr == null) {
return theList;
}
theList.add(curr.getKey());
curr = curr.getNext();
}
return theList;
}
public int size() {
SomeLinkedObject<T> curr = head;
int theSize = 0;
while(curr != null) {
theSize++;
curr = curr.getNext();
}
return theSize;
}
public T get(int index) {
SomeLinkedObject<T> curr = head;
for (int i = 0; i < index; i++) {
if (curr == null) {
return null;
}
curr = curr.getNext();
}
return curr == null ? null : curr.getKey();
}
public void add(T key) {
if(head == null) {
head = new SomeLinkedObject<>(key);
} else {
head = new SomeLinkedObject<>(new SomeLinkedObject<>(key), head.getKey());
}
}
}

View File

@ -0,0 +1,29 @@
package be.brainbaking.lists;
public class SomeLinkedObject<T> {
private final SomeLinkedObject<T> next;
private final T key;
public SomeLinkedObject<T> getNext() {
return next;
}
@Override
public String toString() {
return "[" + key + "]";
}
public T getKey() {
return key;
}
public SomeLinkedObject(T key) {
this(null, key);
}
public SomeLinkedObject(SomeLinkedObject<T> next, T key) {
this.next = next;
this.key = key;
}
}

View File

@ -1,5 +1,7 @@
package be.brainbaking.sorting;
import be.brainbaking.lists.SomeLinkedList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -25,14 +27,14 @@ public class BucketSort implements Sortable {
@Override
public List<Integer> sort(List<Integer> list) {
int n = list.size();
List<List<Integer>> buckets = initializeBuckets(n);
List<SomeLinkedList<Integer>> buckets = initializeBuckets(n);
for(int i = 1; i <= n; i++) {
int x = list.get(i - 1);
int bucketIndex = calculateBucketIndex(n, x);
if(buckets.size() <= bucketIndex) {
buckets.add(bucketIndex, Arrays.asList(x));
buckets.add(bucketIndex, new SomeLinkedList<>(x));
} else {
buckets.get(bucketIndex).add(x);
}
@ -41,20 +43,22 @@ public class BucketSort implements Sortable {
return mergeSortedBuckets(buckets);
}
private List<Integer> mergeSortedBuckets(List<List<Integer>> buckets) {
private List<Integer> mergeSortedBuckets(List<SomeLinkedList<Integer>> buckets) {
List<Integer> sorted = new ArrayList<>();
for (List<Integer> bucket : buckets) {
sorted.addAll(insertionSorter.sort(bucket));
for (SomeLinkedList<Integer> bucket : buckets) {
// crap... I'm too lazy to rewrite insertion sort to use the linkedlist or to implement List<T>. Convert it!
sorted.addAll(insertionSorter.sort(bucket.toList()));
}
return sorted;
}
private List<List<Integer>> initializeBuckets(int n) {
List<List<Integer>> buckets = new ArrayList<>();
private List<SomeLinkedList<Integer>> initializeBuckets(int n) {
List<SomeLinkedList<Integer>> buckets = new ArrayList<>();
for(int i = 0; i < n; i++) {
buckets.add(new ArrayList<>());
buckets.add(new SomeLinkedList<>());
}
return buckets;

View File

@ -0,0 +1,51 @@
package be.brainbaking.lists;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SomeLinkedListTest {
@Test
public void size_newList_isZero() {
SomeLinkedList<Integer> list = new SomeLinkedList<>();
assertEquals(0, list.size());
}
@Test
public void add_increasesSize() {
SomeLinkedList<Integer> list = new SomeLinkedList<>();
list.add(123);
list.add(456);
assertEquals(2, list.size());
}
@Test
public void get_getsByIndex_WithinRange() {
SomeLinkedList<Integer> list = new SomeLinkedList<>();
list.add(123);
list.add(456);
assertEquals(123, (int) list.get(0));
assertEquals(456, (int) list.get(1));
}
@Test
public void get_indexOutOfRange_ReturnsNull_endOfRange() {
SomeLinkedList<Integer> list = new SomeLinkedList<>();
list.add(123);
list.add(456);
assertEquals(null, list.get(2));
}
@Test
public void get_indexOutOfRange_ReturnsNull_emptyList() {
SomeLinkedList<Integer> list = new SomeLinkedList<>();
assertEquals(null, list.get(0));
}
}