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 implements Iterable { private int size = 0; private BodyLinkedListItem first; private BodyLinkedListItem last; /** * Initializes 'this' as an empty list. */ public BodyLinkedList() { first = null; last = null; } /** * Initializes 'this' as an independent copy of the specified list 'list'. * Calling methods of this list will not affect the specified list 'list' * and vice versa. * Precondition: list != null. */ public BodyLinkedList(BodyLinkedList list) { 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) { 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) { if (last == null) { last = new BodyLinkedListItem(body); first = last; } else { last.setNext(new BodyLinkedListItem(body)); last = last.next(); } size++; } /** * Returns the last element in this list. * Returns 'null' if the list is empty. */ public Body getLast() { return (last != null) ? last.body() : null; } /** * Returns the first element in this list. * Returns 'null' if the list is empty. */ public Body getFirst() { return (first != null) ? first.body() : null; } /** * Retrieves and removes the first element in this list. * Returns 'null' if the list is empty. */ public Body pollFirst() { if (first == null) { return null; } Body b = first.body(); first = first.next(); if (first != null) first.setPrev(null); size--; return b; } /** * Retrieves and removes the last element in this list. * Returns 'null' if the list is empty. */ public Body pollLast() { if (last == null) { return null; } Body b = last.body(); last = last.prev(); if (last != null) last.setNext(null); size--; return b; } /** * Inserts the specified element 'body' at the specified position in this list. * Precondition: i >= 0 && i <= size(). */ public void add(int i, Body body) { 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(); } /** * Returns the element at the specified position in this list. * Precondition: i >= 0 && i < size(). */ public Body get(int i) { 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(); } /** * Returns the index of the first occurrence of the specified element in this list, or -1 if * this list does not contain the element. */ public int indexOf(Body body) { 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; } /** * Removes all bodies of this list, which are colliding with the specified * body. Returns a list with all the removed bodies. */ public BodyLinkedList removeCollidingWith(Body body) { 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() { return size; } @Override public Iterator 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(); } }; } } class BodyLinkedListItem { private final Body body; private BodyLinkedListItem prev; private BodyLinkedListItem next; public BodyLinkedListItem(Body body) { this.body = body; this.prev = null; this.next = null; } public Body body() { return body; } public BodyLinkedListItem prev() { return prev; } public void setPrev(BodyLinkedListItem prev) { this.prev = prev; if (prev != null) prev.next = this; } public BodyLinkedListItem next() { return next; } public void setNext(BodyLinkedListItem next) { this.next = next; if (next != null) next.prev = this; } }