1
0
forked from pabulaner/vivi

add, set, get, size, clear, toString

This commit is contained in:
happymeal2024 2025-04-11 19:15:33 +02:00
parent 5a7a6e8a56
commit acc84590fd
7 changed files with 73 additions and 35 deletions

View File

@ -5,24 +5,26 @@ import java.util.*;
public class ArrayByteList implements ByteList { public class ArrayByteList implements ByteList {
private int size = 0; private int size = 0;
byte[] array; private byte[] array;
public ArrayByteList() {
array = new byte[0];
}
@Override @Override
public void add(byte value) { public void add(byte value) {
add(size(), value);
++size; ++size;
if (size == 1) { if (size == 1) {
byte[] oldArray = new byte[size]; byte[] oldArray = new byte[size];
oldArray[size - 1] = value; oldArray[size - 1] = value;
array = oldArray; array = oldArray;
//System.out.println(Arrays.toString(arrayOld));
} else { } else {
byte[] newArray = new byte[size]; byte[] newArray = new byte[size];
System.arraycopy(array, 0, newArray, 0, size - 1); System.arraycopy(array, 0, newArray, 0, size - 1);
newArray[size - 1] = value; newArray[size - 1] = value;
array = newArray; array = newArray;
//System.out.println(Arrays.toString(arrayNew));
} }
//System.out.println(Arrays.toString(array));
} }
@Override @Override

View File

@ -6,7 +6,6 @@ import java.util.function.Consumer;
public class ArrayByteListIterator implements Iterator<Byte> { public class ArrayByteListIterator implements Iterator<Byte> {
private ArrayByteList array; private ArrayByteList array;
// private Byte nextByte;
private int currentIndex = 0; private int currentIndex = 0;
public ArrayByteListIterator(ArrayByteList array) { public ArrayByteListIterator(ArrayByteList array) {
@ -20,11 +19,6 @@ public class ArrayByteListIterator implements Iterator<Byte> {
@Override @Override
public Byte next() { public Byte next() {
// if (hasNext()) {
// //nextByte = array.get(currentIndex++);
// } else {
// nextByte = null;
// }
return array.get(currentIndex++); return array.get(currentIndex++);
} }

View File

@ -1,7 +1,5 @@
package de.vivi.list; package de.vivi.list;
import java.util.Iterator;
public interface ByteList extends Iterable<Byte> { public interface ByteList extends Iterable<Byte> {
void add(byte value); void add(byte value);

View File

@ -1,52 +1,67 @@
package de.vivi.list; package de.vivi.list;
import java.util.Iterator; import java.util.*;
public class LinkedByteList implements ByteList { public class LinkedByteList implements ByteList {
public NodeLinked head; private NodeLinked head;
public NodeLinked tail; private NodeLinked currNode;
private int size;
public LinkedByteList() { public LinkedByteList() {
this.head = null; head = null;
this.tail = null;
} }
@Override @Override
public void add(byte value) { public void add(byte value) {
NodeLinked new_Node = new NodeLinked(value); NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node
if (head == null) { if (head == null) {
head = new_Node; head = newNode;
tail = new_Node;
} else { } else {
tail.next = new_Node; NodeLinked last = head;
tail = new_Node; while (last.next != null) {
last = last.next;
}
last.next = newNode;
} }
size++;
} }
@Override @Override
public void add(int index, byte value) { public void add(int index, byte value) {
if (index == size) {
}
} }
@Override @Override
public void remove(int index) { public void remove(int index) {
size--;
} }
@Override @Override
public void set(int index, byte value) { public void set(int index, byte value) {
NodeLinked x = node(index);
x.value = value;
}
NodeLinked node(int index) {
NodeLinked x = head;
for (int i = 0; i < index; i++) {
x = x.next;
}
return x;
} }
@Override @Override
public byte get(int index) { public byte get(int index) {
return 0; return node(index).value;
} }
@Override @Override
public int size() { public int size() {
return 0; return size;
} }
@Override @Override
@ -61,7 +76,13 @@ public class LinkedByteList implements ByteList {
@Override @Override
public void clear() { public void clear() {
for (NodeLinked x = head; x != null;) {
NodeLinked next = x.next;
x.next = null;
x = next;
}
head = null;
size = 0;
} }
@Override @Override
@ -69,5 +90,23 @@ public class LinkedByteList implements ByteList {
return null; return null;
} }
public Byte[] toArray() {
Byte[] result = new Byte[size];
int i = 0;
for (NodeLinked x = head; x != null; x = x.next) {
result[i++] = x.value;
}
return result;
}
@Override
public String toString() {
System.out.print("LinkedList: ");
currNode = head;
while (currNode != null) {
System.out.print(currNode.value + " ");
currNode = currNode.next;
}
return String.valueOf(currNode).replace("null", "");
}
} }

View File

@ -0,0 +1,6 @@
package de.vivi.list;
public class LinkedByteListIterator {
public LinkedByteListIterator(LinkedByteList bytes) {
}
}

View File

@ -28,11 +28,12 @@ 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) 5); list.add((byte) 5);
// list.add((byte) -100); list.add((byte) -100);
// System.out.println(list); System.out.println(list);
// list.add(0, (byte) 99);
// list.add(3, (byte) 99); // list.add(3, (byte) 99);
//// System.out.println(list); //// System.out.println(list);
// list.add(2, (byte) -40); // list.add(2, (byte) -40);
@ -44,7 +45,7 @@ public class Main {
// System.out.println(list); // System.out.println(list);
// list.set(1, (byte) 43); // list.set(1, (byte) 43);
System.out.println(list); // System.out.println(list);
// list.set(0, (byte) 8); // list.set(0, (byte) 8);
//// System.out.println(list); //// System.out.println(list);
// //

View File

@ -2,13 +2,11 @@ package de.vivi.list;
public class NodeLinked { public class NodeLinked {
byte data; byte value;
NodeLinked next; NodeLinked next;
NodeLinked prev;
public NodeLinked(byte data) { public NodeLinked(byte value) {
this.data = data; this.value = value;
this.next = null; this.next = null;
this.prev = null;
} }
} }