1
0
forked from pabulaner/vivi

LinkedList finished

This commit is contained in:
happymeal2024 2025-04-18 14:02:12 +02:00
parent 657bcf5463
commit e28f39412b
6 changed files with 40 additions and 40 deletions

View File

@ -30,10 +30,8 @@ public class ArrayByteList implements ByteList {
@Override
public void add(int index, byte value) {
byte[] newArray = new byte[array.length + 1];
System.arraycopy(array, 0, newArray, 0, array.length);
for (int i = newArray.length - 1; i >= index; i--) {
newArray[i] = newArray[i - 1];
}
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index, newArray, index + 1, size - index);
newArray[index] = value;
array = newArray;
}
@ -82,7 +80,7 @@ public class ArrayByteList implements ByteList {
}
@Override
public ArrayByteList copy() {
public ByteList copy() {
ArrayByteList newArrayList = new ArrayByteList();
newArrayList.array = Arrays.copyOf(array, array.length);
return newArrayList;

View File

@ -21,14 +21,4 @@ public class ArrayByteListIterator implements Iterator<Byte> {
public Byte next() {
return array.get(currentIndex++);
}
@Override
public void remove() {
Iterator.super.remove();
}
@Override
public void forEachRemaining(Consumer action) {
Iterator.super.forEachRemaining(action);
}
}

View File

@ -18,7 +18,7 @@ public interface ByteList extends Iterable<Byte> {
String toString();
ArrayByteList copy();
ByteList copy();
void clear();
}

View File

@ -83,19 +83,6 @@ public class LinkedByteList implements ByteList {
}
return current;
}
// NodeLinked node(int index) {
// if (index < (size >> 1)) {
// NodeLinked x = first;
// for (int i = 0; i < index; i++)
// x = x.next;
// return x;
// } else {
// NodeLinked x = last;
// for (int i = size - 1; i > index; i--)
// x = x.prev;
// return x;
// }
// }
@Override
public byte get(int index) {
@ -123,8 +110,15 @@ public class LinkedByteList implements ByteList {
}
@Override
public ArrayByteList copy() {
return null;
public ByteList copy() {
LinkedByteList clone = new LinkedByteList();
clone.first = clone.last = null;
clone.size = 0;
for (NodeLinked x = first; x != null; x = x.next) {
clone.add(x.item);
}
return clone;
}
@Override
@ -139,8 +133,9 @@ public class LinkedByteList implements ByteList {
}
@Override
public Iterator<Byte> iterator() {
return null;
public LinkedByteListIterator iterator() {
LinkedByteListIterator it = new LinkedByteListIterator(this);
return it;
}
@Override

View File

@ -1,6 +1,23 @@
package de.vivi.list;
public class LinkedByteListIterator {
public LinkedByteListIterator(LinkedByteList bytes) {
import java.util.Iterator;
import java.util.function.Consumer;
public class LinkedByteListIterator implements Iterator<Byte> {
private LinkedByteList linkedList;
private int currentIndex;
public LinkedByteListIterator(LinkedByteList linkedList) {
this.linkedList = linkedList;
}
@Override
public boolean hasNext() {
return currentIndex < linkedList.size();
}
@Override
public Byte next() {
return linkedList.get(currentIndex++);
}
}

View File

@ -33,10 +33,10 @@ public class Main {
System.out.println(list);
System.out.println(list.copy());
// list.remove(4);
// list.contains(5);
System.out.println(list.contains(2));
// System.out.println(list.contains(2));
// list.add(0, (byte) 99);
// list.add(3, (byte) 99);
@ -79,9 +79,9 @@ public class Main {
// list.clear();
// System.out.println(list);
// for (byte value : list) {
// System.out.print("Hello it " + value + " ");
// }
for (byte value : list) {
System.out.print(value + " ");
}
}