AB3: BodyLinkedList working

This commit is contained in:
2022-03-31 19:50:10 +02:00
parent cf4c1ad9d0
commit f24ad9bcaf
7 changed files with 252 additions and 51 deletions

View File

@ -1,16 +1,20 @@
import java.util.Iterator;
/**
* A list of bodies implemented as a linked list.
* The number of elements of the list is not limited.
*/
public class BodyLinkedList {
//TODO: declare variables.
public class BodyLinkedList implements Iterable<Body> {
private int size = 0;
private BodyLinkedListItem first;
private BodyLinkedListItem last;
/**
* Initializes 'this' as an empty list.
*/
public BodyLinkedList() {
//TODO: define constructor.
first = null;
last = null;
}
/**
@ -20,21 +24,38 @@ public class BodyLinkedList {
* Precondition: list != null.
*/
public BodyLinkedList(BodyLinkedList list) {
//TODO: define constructor.
this.size = 0;
for (Body b : list) {
this.addLast(b);
}
}
/**
* Inserts the specified element 'body' at the beginning of this list.
*/
public void addFirst(Body body) {
//TODO: implement method.
if (first == null) {
first = new BodyLinkedListItem(body);
last = first;
} else {
first.setPrev(new BodyLinkedListItem(body));
first = first.prev();
}
size++;
}
/**
* Appends the specified element 'body' to the end of this list.
*/
public void addLast(Body body) {
//TODO: implement method.
if (last == null) {
last = new BodyLinkedListItem(body);
first = last;
} else {
last.setNext(new BodyLinkedListItem(body));
last = last.next();
}
size++;
}
/**
@ -42,8 +63,7 @@ public class BodyLinkedList {
* Returns 'null' if the list is empty.
*/
public Body getLast() {
//TODO: implement method.
return null;
return (last != null) ? last.body() : null;
}
/**
@ -51,8 +71,7 @@ public class BodyLinkedList {
* Returns 'null' if the list is empty.
*/
public Body getFirst() {
//TODO: implement method.
return null;
return (first != null) ? first.body() : null;
}
/**
@ -60,8 +79,14 @@ public class BodyLinkedList {
* Returns 'null' if the list is empty.
*/
public Body pollFirst() {
//TODO: implement method.
return null;
if (first == null) {
return null;
}
Body b = first.body();
first = first.next();
if (first != null) first.setPrev(null);
size--;
return b;
}
/**
@ -69,8 +94,14 @@ public class BodyLinkedList {
* Returns 'null' if the list is empty.
*/
public Body pollLast() {
//TODO: implement method.
return null;
if (last == null) {
return null;
}
Body b = last.body();
last = last.prev();
if (last != null) last.setNext(null);
size--;
return b;
}
/**
@ -78,7 +109,36 @@ public class BodyLinkedList {
* Precondition: i >= 0 && i <= size().
*/
public void add(int i, Body body) {
//TODO: implement method.
if (first == null || i == 0) {
addFirst(body);
return;
} else if (i == size) {
addLast(body);
return;
}
BodyLinkedListItem item = first;
for (int j = 0; j < i; j++) {
item = item.next();
}
item.prev().setNext(new BodyLinkedListItem(body));
item.setPrev(item.prev().next());
size++;
}
private Body removeItem(BodyLinkedListItem item) {
if (item == first) {
first = item.next();
if (first != null) first.setPrev(null);
} else if (item == last) {
last = item.prev();
if (last != null) last.setNext(null);
} else {
item.next().setPrev(item.prev());
}
size--;
return item.body();
}
/**
@ -86,8 +146,19 @@ public class BodyLinkedList {
* Precondition: i >= 0 && i < size().
*/
public Body get(int i) {
//TODO: implement method.
return null;
BodyLinkedListItem item;
if (i < size / 2) {
item = first;
for (int j = 0; j < i; j++) {
item = item.next();
}
} else {
item = last;
for (int j = size - 1; j > i; j--) {
item = item.prev();
}
}
return item.body();
}
/**
@ -95,8 +166,19 @@ public class BodyLinkedList {
* this list does not contain the element.
*/
public int indexOf(Body body) {
//TODO: implement method.
return -2;
if (first == null) {
return -1;
}
BodyLinkedListItem item = first;
for (int i = 0; i < size; i++) {
if (item.body() == body) {
return i;
}
item = item.next();
}
return -1;
}
/**
@ -104,15 +186,42 @@ public class BodyLinkedList {
* body. Returns a list with all the removed bodies.
*/
public BodyLinkedList removeCollidingWith(Body body) {
//TODO: implement method.
return null;
BodyLinkedList removed = new BodyLinkedList();
for (BodyLinkedListItem item = first; item != null; item = item.next()) {
if (body != item.body() && body.collidesWith(item.body())) {
removed.addLast(this.removeItem(item));
}
}
return removed;
}
/**
* Returns the number of bodies in this list.
*/
public int size() {
//TODO: implement method.
return -1;
return size;
}
@Override
public Iterator<Body> iterator() {
return new Iterator<>() {
BodyLinkedListItem ptr = first;
boolean yieldedFirst = false;
@Override
public boolean hasNext() {
return ptr != null && (!yieldedFirst || ptr.next() != null);
}
@Override
public Body next() {
if (!yieldedFirst) {
yieldedFirst = true;
return ptr.body();
}
ptr = ptr.next();
return ptr.body();
}
};
}
}