1
0
forked from pabulaner/vivi

Compare commits

..

No commits in common. "44a7e7479eeba5793794304b6153fbf846fa35e1" and "321a924555bbd3502f24f779878e2acb694b2df1" have entirely different histories.

3 changed files with 127 additions and 112 deletions

View File

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

View File

@ -1,15 +1,18 @@
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); NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node
if (first == null) { if (first == null) {
first = newNode; first = newNode;
} else { } else {
@ -24,29 +27,31 @@ public class LinkedByteList implements ByteList {
@Override @Override
public void add(int index, byte value) { public void add(int index, byte value) {
if (index == 0) { if (index == size) {
NodeLinked newNode = new NodeLinked(value); linkLast(value);
if (size == 0) {
last = newNode;
} else {
newNode.next = first;
}
first = newNode;
size++;
} else if (index == size) {
NodeLinked newNode = new NodeLinked(value);
last.next = newNode;
last = newNode;
size++;
} else { } else {
NodeLinked newNode = new NodeLinked(value); linkBefore(value, node(index));
NodeLinked prev = getCurrNode(index - 1);
newNode.next = prev.next;
prev.next = newNode;
size++;
} }
} }
void linkBefore(byte value, NodeLinked succ) {
NodeLinked pred = succ.prev;
NodeLinked newNode = new NodeLinked(value, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
}
void linkLast(byte value) {
NodeLinked l = last;
NodeLinked newNode = new NodeLinked(value);
last = newNode;
size++;
}
@Override @Override
public void remove(int index) { public void remove(int index) {
NodeLinked current = first; NodeLinked current = first;
@ -84,17 +89,6 @@ 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;
@ -146,8 +140,7 @@ public class LinkedByteList implements ByteList {
@Override @Override
public String toString() { public String toString() {
System.out.println(); System.out.print("LinkedList: ");
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,23 +1,25 @@
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);
} }
@ -26,32 +28,57 @@ 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); //
list.add(2, (byte) -40); // System.out.println(list);
// System.out.println(list.copy());
// list.remove(4);
// list.contains(5);
// System.out.println(list.contains(2));
list.remove(2); // list.add(0, (byte) 99);
list.remove(3); // list.add(3, (byte) 99);
//// System.out.println(list);
// list.add(2, (byte) -40);
// System.out.println(list);
list.set(1, (byte) 43); // list.remove(2);
list.set(0, (byte) 8); //// System.out.println(list);
// list.remove(3);
// System.out.println(list);
list.get(2); // list.set(1, (byte) 43);
list.get(1); // System.out.println(list);
// 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));
list.size(); // String s = list.toString();
// System.out.println(s);
list.contains(43); // list.copy();
list.contains(4); // System.out.println(list.copy());
String s = list.toString(); // ByteList list2 = list.copy();
System.out.println(s); // list2.set(0, (byte) 6);
// 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 + " ");
@ -59,9 +86,7 @@ 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);
@ -107,21 +132,14 @@ 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) {
System.out.print(value + " "); // do some stuff with value...
} }
} }
private static void testSort(ByteList list) { private static void testSort(ByteList list) {
@ -130,19 +148,12 @@ 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();
@ -150,15 +161,13 @@ 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()));
} }
} }