1
0
forked from pabulaner/vivi
This commit is contained in:
happymeal2024 2025-05-25 16:53:39 +02:00
parent 82e4b75df0
commit f3ca7ff227
3 changed files with 113 additions and 128 deletions

View File

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

View File

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

View File

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