Solved Task 3

This commit is contained in:
schrom01 2022-09-27 16:57:48 +02:00
parent 6ee5e173c4
commit 6c80c7ae06
2 changed files with 172 additions and 2 deletions

View File

@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* @author * @author
* @version 1.00 2017/8/30 * @version 1.00 2017/8/30
*/ */
/**
public class ADS2_3_test { public class ADS2_3_test {
MyList list; MyList list;
@ -98,4 +98,3 @@ public class ADS2_3_test {
} }
} }
} }
*/

171
src/ch/zhaw/ads/MyList.java Normal file
View File

@ -0,0 +1,171 @@
package ch.zhaw.ads;
import java.util.AbstractList;
import java.util.Comparator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
public class MyList extends AbstractList {
MyNode head;
MyNode tail;
@Override
public boolean add(Object o) {
if(head == null)
{
head = new MyNode(null, null, o);
tail = head;
} else {
MyNode actualNode = head;
while (actualNode.getNextNode() != null) {
actualNode = actualNode.getNextNode();
}
actualNode.setNextNode(new MyNode(null, actualNode, o));
tail = actualNode.getNextNode();
}
return true;
}
@Override
public boolean remove(Object o){
MyNode actualNode = head;
while(actualNode != null) {
if(actualNode.getData() == o){
MyNode previousNode = actualNode.getPreviousNode();
MyNode nextNode = actualNode.getNextNode();
if(previousNode == null) {
head = nextNode;
} else {
previousNode.setNextNode(nextNode);
}
if(nextNode == null) {
tail = previousNode;
} else {
nextNode.setPreviousNode(previousNode);
}
return true;
}
actualNode = actualNode.getNextNode();
}
return false;
}
@Override
public Object get(int index) {
MyNode actualNode = head;
for(int i = 0; i < index; i++) {
if(actualNode != null) {
actualNode = actualNode.nextNode;
} else {
throw new IndexOutOfBoundsException();
}
}
return actualNode.getData();
}
@Override
public boolean isEmpty() {
return head == null;
}
@Override
public void clear(){
head = null;
tail = null;
}
@Override
public void forEach(Consumer action) {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public Spliterator spliterator() {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public Stream stream() {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public Stream parallelStream() {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public int size() {
if(head == null){
return 0;
}
MyNode actualNode = head;
int count = 1;
for(; actualNode.getNextNode() != null; count++){
actualNode = actualNode.getNextNode();
}
return count;
}
@Override
public boolean removeIf(Predicate filter) {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public Object[] toArray(IntFunction generator) {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public void replaceAll(UnaryOperator operator) {
throw new UnsupportedOperationException("Method not implemented!");
}
@Override
public void sort(Comparator c) {
throw new UnsupportedOperationException("Method not implemented!");
}
private class MyNode {
private MyNode nextNode;
private MyNode previousNode;
private Object data;
MyNode(MyNode nextNode, MyNode previousNode, Object data){
setNextNode(nextNode);
setPreviousNode(previousNode);
setData(data);
}
public void setNextNode(MyNode nextNode) {
this.nextNode = nextNode;
}
public MyNode getNextNode() {
return nextNode;
}
public void setPreviousNode(MyNode previousNode) {
this.previousNode = previousNode;
}
public MyNode getPreviousNode() {
return previousNode;
}
public void setData(Object data) {
this.data = data;
}
public Object getData() {
return data;
}
}
}