Полностью реализовать интерфейс java.util.List я не успеваю. Не реализованные методы будут выбрасывать исключение UnsupportedOperationException, если надо могу их потом реализовать. Но базовое условие задачи я сделал.
| Код | import java.util.*;
public class ReversingList<E> implements List<E> { private Entry<E> head; private int size = 0;
public ReversingList() { }
public static void main(String[] args) { ReversingList<String> list = new ReversingList<String>(); list.add("A"); list.add("B"); list.add(1, "C"); list.add(list.size(), "D"); list.add(list.size(), "A"); System.out.println("list.size() = " + list.size());
printCollection(list);
System.out.println("list.indexOf(\"A\") = " + list.indexOf("A")); System.out.println("list.indexOf(\"B\") = " + list.indexOf("B")); System.out.println("list.indexOf(\"C\") = " + list.indexOf("C")); System.out.println("list.indexOf(\"D\") = " + list.indexOf("D")); System.out.println("list.indexOf(\"E\") = " + list.indexOf("E")); System.out.println("list.lastIndexOf(\"A\") = " + list.lastIndexOf("A"));
list.reverse(); printCollection(list); }
private static void printCollection(List list) { System.out.print("["); for(Object aList : list) System.out.print(aList + " "); System.out.println("]"); }
private void checkBounds(int index) { if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Invalid index: " + index); }
public void reverse() { if(head == null) return;
Entry<E> h = head; Entry<E> t = head; while(t.next != null) t = t.next;
while(h != t) { E e = h.element; h.element = t.element; t.element = e; if(h.next == t) break; h = h.next; t = t.previous; } }
public int size() { return size; }
public boolean isEmpty() { return size() == 0; }
public boolean add(E e) { add(0, e); return true; }
public void add(int index, E element) { if(index > size || index < 0) throw new IndexOutOfBoundsException("Invalid index: " + index);
if(index == 0) { Entry<E> newEntry = new Entry<E>(element, head, null); if(head != null) head.previous = newEntry; head = newEntry; } else { int i = 0; Entry<E> current = head; while(i < index && current.next != null) { current = current.next; i++; }
if(i == index) { Entry<E> newEntry = new Entry<E>(element, current, current.previous); if(newEntry.next != null) newEntry.next.previous = newEntry; if(newEntry.previous != null) newEntry.previous.next = newEntry; } else { Entry<E> newEntry = new Entry<E>(element, null, current); if(newEntry.previous != null) newEntry.previous.next = newEntry; } } size++; }
public E get(int index) { checkBounds(index);
int i = 0; Entry<E> current = head; while(i < index) { current = current.next; i++; } return current.element; }
public E set(int index, E element) { checkBounds(index);
int i = 0; Entry<E> current = head; while(i < index) { current = current.next; i++; }
E old = current.element; current.element = element; return old; }
public E remove(int index) { checkBounds(index);
int i = 0; Entry<E> entry = head; while(i < index) { entry = entry.next; i++; }
if(entry.previous != null) entry.previous.next = entry.next; if(entry.next != null) entry.next.previous = entry.previous; if(index == 0) head = entry.next;
size--; return entry.element; }
public boolean remove(Object o) { int index; boolean removed = false; while((index = indexOf(o)) != -1) { remove(index); removed = true; } return removed; }
public void clear() { head = null; size = 0; }
public int indexOf(Object o) { int i = 0; Entry<E> entry = head; while(entry != null) { if(entry.element != null) { if(entry.element.equals(o)) return i; } else { if(o == null) return i; } entry = entry.next; i++; } return -1; }
public int lastIndexOf(Object o) { int current = 0; int last = -1;
Entry<E> entry = head; while(entry != null) { if(entry.element != null) { if(entry.element.equals(o)) last = current; } else { if(o == null) last = current; } entry = entry.next; current++; }
return last; }
public boolean contains(Object o) { return indexOf(o) != -1; }
public boolean containsAll(Collection<?> c) { for(Object o : c) { if(!contains(o)) return false; } return true; }
public boolean addAll(int index, Collection<? extends E> c) { for(E aC : c) { add(index, aC); index++; } return c.size() > 0; }
public boolean addAll(Collection<? extends E> c) { return addAll(0, c); }
public boolean removeAll(Collection<?> c) { for(Object o : c) remove(o); return c.size() > 0; }
//////////////////// NOT IMPLEMENTED METHODS ////////////////////
public Iterator<E> iterator() { return listIterator(); }
public Object[] toArray() { throw new UnsupportedOperationException(); }
public <T> T[] toArray(T[] a) { throw new UnsupportedOperationException(); }
public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
public ListIterator<E> listIterator() { throw new UnsupportedOperationException(); }
public ListIterator<E> listIterator(int index) { throw new UnsupportedOperationException(); }
public List<E> subList(int fromIndex, int toIndex) { throw new UnsupportedOperationException(); }
private static class Entry<E> { public E element; public Entry<E> next; public Entry<E> previous;
public Entry(E element, Entry<E> next, Entry<E> previous) { this.element = element; this.next = next; this.previous = previous; }
@Override public String toString() { return "Element[" + element + "]" + "(next = " + (next == null ? "null" : next.element) + " , previous = " + (previous == null ? "null" : previous.element) + ")"; } } } |
P.S. Код расчитан на JDK 1.5. |