1
0
forked from pabulaner/vivi

Compare commits

...

2 Commits

3 changed files with 113 additions and 128 deletions

View File

@ -5,92 +5,79 @@ import java.util.*;
public class ArrayByteList implements ByteList { public class ArrayByteList implements ByteList {
private int size = 0; private int size = 0;
private byte[] array; private byte[] values;
public ArrayByteList() { public ArrayByteList() {
array = new byte[0]; values = new byte[0];
} }
@Override @Override
public void add(byte value) { public void add(byte value) {
add(size(), value); add(size(), value);
++size;
if (size == 1) {
byte[] oldArray = new byte[size];
oldArray[size - 1] = value;
array = oldArray;
} else {
byte[] newArray = new byte[size];
System.arraycopy(array, 0, newArray, 0, size - 1);
newArray[size - 1] = value;
array = newArray;
}
} }
@Override @Override
public void add(int index, byte value) { public void add(int index, byte value) {
byte[] newArray = new byte[array.length + 1]; byte[] newValues = new byte[size() + 1];
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index, newArray, index + 1, size - index); System.arraycopy(values, 0, newValues, 0, index);
newArray[index] = value; System.arraycopy(values, index, newValues, index + 1, size() - index);
array = newArray;
newValues[index] = value;
values = newValues;
} }
@Override @Override
public void remove(int index) { public void remove(int index) {
for (int i = index; i < array.length - 1; i++) { byte[] newValues = new byte[size() - 1];
array[i] = array[i + 1];
} System.arraycopy(values, 0, newValues, 0, index);
byte[] newArray = new byte[array.length - 1]; System.arraycopy(values, index + 1, newValues, index, size() - index - 1);
System.arraycopy(array, 0, newArray, 0, array.length - 1);
array = newArray; values = newValues;
} }
@Override @Override
public void set(int index, byte value) { public void set(int index, byte value) {
byte[] newArray = new byte[array.length]; values[index] = value;
System.arraycopy(array, 0, newArray, 0, array.length);
newArray[index] = value;
array = newArray;
} }
@Override @Override
public byte get(int index) { public byte get(int index) {
return array[index]; return values[index];
} }
@Override @Override
public int size() { public int size() {
return array.length; return values.length;
} }
@Override @Override
public boolean contains(int value) { public boolean contains(int value) {
boolean isIndexInArray = false; boolean isIndexInArray = false;
for (int i = 0; i < array.length; i++) { for (int i = 0; i < values.length; i++) {
if (array[i] == value) { if (values[i] == value) {
isIndexInArray = true; isIndexInArray = true;
} }
} }
return isIndexInArray; return isIndexInArray;
} }
@Override
public String toString() { public String toString() {
return Arrays.toString(array); return "ArrayByteList " + Arrays.toString(values);
} }
@Override @Override
public ByteList copy() { public ByteList copy() {
ArrayByteList newArrayList = new ArrayByteList(); ArrayByteList newArrayList = new ArrayByteList();
newArrayList.array = Arrays.copyOf(array, array.length); newArrayList.values = Arrays.copyOf(values, values.length);
return newArrayList; return newArrayList;
} }
@Override @Override
public void clear() { public void clear() {
byte[] newArray = new byte[0]; values = new byte[0];
System.arraycopy(array, 0, newArray, 0, 0);
array = newArray;
} }
@Override @Override

View File

@ -1,18 +1,15 @@
package de.vivi.list; package de.vivi.list;
import java.util.*;
public class LinkedByteList implements ByteList { public class LinkedByteList implements ByteList {
private NodeLinked currNode; private NodeLinked currNode;
private int size; private int size;
private NodeLinked last; private NodeLinked last;
private NodeLinked first; private NodeLinked first;
int result;
@Override @Override
public void add(byte value) { public void add(byte value) {
NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node NodeLinked newNode = new NodeLinked(value);
if (first == null) { if (first == null) {
first = newNode; first = newNode;
} else { } else {
@ -27,29 +24,27 @@ public class LinkedByteList implements ByteList {
@Override @Override
public void add(int index, byte value) { public void add(int index, byte value) {
if (index == size) { if (index == 0) {
linkLast(value); NodeLinked newNode = new NodeLinked(value);
} else { if (size == 0) {
linkBefore(value, node(index)); last = newNode;
} } else {
} newNode.next = first;
}
void linkBefore(byte value, NodeLinked succ) {
NodeLinked pred = succ.prev;
NodeLinked newNode = new NodeLinked(value, succ);
succ.prev = newNode;
if (pred == null)
first = newNode; first = newNode;
else size++;
pred.next = newNode; } else if (index == size) {
size++; NodeLinked newNode = new NodeLinked(value);
} last.next = newNode;
last = newNode;
void linkLast(byte value) { size++;
NodeLinked l = last; } else {
NodeLinked newNode = new NodeLinked(value); NodeLinked newNode = new NodeLinked(value);
last = newNode; NodeLinked prev = getCurrNode(index - 1);
size++; newNode.next = prev.next;
prev.next = newNode;
size++;
}
} }
@Override @Override
@ -89,6 +84,17 @@ public class LinkedByteList implements ByteList {
return node(index).item; return node(index).item;
} }
public NodeLinked getCurrNode(int index) {
if (index < 0 || index >= size) {
return null;
}
NodeLinked current = first;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
@Override @Override
public int size() { public int size() {
return size; return size;
@ -140,7 +146,8 @@ public class LinkedByteList implements ByteList {
@Override @Override
public String toString() { public String toString() {
System.out.print("LinkedList: "); System.out.println();
System.out.println("LinkedList: ");
currNode = first; currNode = first;
while (currNode != null) { while (currNode != null) {
System.out.print(currNode.item + " "); System.out.print(currNode.item + " ");

View File

@ -1,25 +1,23 @@
package de.vivi.list; package de.vivi.list;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ByteList array = new ArrayByteList(); ByteList array = new ArrayByteList();
ByteList linked = new LinkedByteList(); ByteList linked = new LinkedByteList();
// ByteList combined = new CombinedByteList(16); // Paul meinte das kann weg gelassen werden // ByteList combined = new CombinedByteList(16); // Paul meinte das kann weg gelassen werden
testListVivi(array); // testListVivi(array);
//testListVivi(linked); // testListVivi(linked);
testSortVivi(array); // testSortVivi(array);
//testList(array); testList(array);
// testList(linked); testList(linked);
// testSortVivi(array);
// testSortVivi(linked);
// testList(combined); // testList(combined);
// testSort(array); testSort(array);
// testSort(linked); testSort(linked);
// testSort(combined); // testSort(combined);
} }
@ -28,57 +26,32 @@ public class Main {
list.add((byte) 5); list.add((byte) 5);
list.add((byte) -86); list.add((byte) -86);
list.add((byte) 3); list.add((byte) 3);
list.add((byte) 55);
list.add((byte) -100);
list.add((byte) -20);
// list.add(3, (byte) 99);
// System.out.println(list); list.add(2, (byte) -40);
// System.out.println(list.copy());
// list.remove(4);
// list.contains(5);
// System.out.println(list.contains(2));
// list.add(0, (byte) 99); list.remove(2);
// list.add(3, (byte) 99); list.remove(3);
//// System.out.println(list);
// list.add(2, (byte) -40);
// System.out.println(list);
// list.remove(2); list.set(1, (byte) 43);
//// System.out.println(list); list.set(0, (byte) 8);
// list.remove(3);
// System.out.println(list);
// list.set(1, (byte) 43); list.get(2);
// System.out.println(list); list.get(1);
// list.set(0, (byte) 8);
//// System.out.println(list);
//
// list.get(2);
//// System.out.println(list.get(2));
// list.get(1);
//// System.out.println(list.get(1));
//
// list.size();
// System.out.println(list.size());
// list.contains(43); // returns: true
//// System.out.println(list.contains(43));
// list.contains(4);
// System.out.println(list.contains(4));
// String s = list.toString(); list.size();
// System.out.println(s);
// list.copy(); list.contains(43);
// System.out.println(list.copy()); list.contains(4);
// ByteList list2 = list.copy(); String s = list.toString();
// list2.set(0, (byte) 6); System.out.println(s);
// System.out.println(list2);
list.copy();
System.out.println(list.copy());
// list.clear(); // list.clear();
// System.out.println(list); // System.out.println(list);
for (byte value : list) { for (byte value : list) {
System.out.print(value + " "); System.out.print(value + " ");
@ -86,7 +59,9 @@ public class Main {
} }
/*private static void testList(ByteList list) {
private static void testList(ByteList list) {
// add the element at the end // add the element at the end
list.add((byte) 10); list.add((byte) 10);
list.add((byte) 5); list.add((byte) 5);
@ -132,14 +107,21 @@ public class Main {
// remove all elements inside the list // remove all elements inside the list
list.clear(); // list.clear();
// list: [] // list: []
// allow to use for each loop on list // allow to use for each loop on list
// for (byte value : list) {
// // do some stuff with value...
// }
System.out.println();
System.out.println("List ");
for (byte value : list) { for (byte value : list) {
// do some stuff with value... System.out.print(value + " ");
} }
} }
private static void testSort(ByteList list) { private static void testSort(ByteList list) {
@ -148,12 +130,19 @@ public class Main {
ByteSort bogo = new BogoSort(); ByteSort bogo = new BogoSort();
// sort the lists with the sorting algorithm // sort the lists with the sorting algorithm
System.out.println();
bubble.sort(list.copy()); bubble.sort(list.copy());
System.out.println("bubble ");
System.out.println(bubble.sort(list.copy()));
merge.sort(list.copy()); merge.sort(list.copy());
System.out.println("merge ");
System.out.println(merge.sort(list.copy()));
bogo.sort(list.copy()); bogo.sort(list.copy());
System.out.println("bogo ");
System.out.println(bogo.sort(list.copy()));
} }
*/
private static void testSortVivi(ByteList list) { private static void testSortVivi(ByteList list) {
ByteSort bubble = new BubbleSort(); ByteSort bubble = new BubbleSort();
@ -161,13 +150,15 @@ public class Main {
ByteSort bogo = new BogoSort(); ByteSort bogo = new BogoSort();
// sort the lists with the sorting algorithm // sort the lists with the sorting algorithm
// bubble.sort(list.copy()); bubble.sort(list.copy());
// System.out.println("bubblesort " + bubble.sort(list.copy()));
// System.out.println();
// System.out.println(merge.sort(list.copy()));
// merge.sort(list.copy());
// bogo.sort(list.copy());
System.out.println(); System.out.println();
System.out.println();
System.out.println("bubblesort " + bubble.sort(list.copy()));
System.out.println(merge.sort(list.copy()));
merge.sort(list.copy());
bogo.sort(list.copy());
System.out.println(bogo.sort(list.copy())); System.out.println(bogo.sort(list.copy()));
} }
} }