class MyArrayList8 { // Doubly linked list implementation of ArrayList private Cell front,back; private int mySize; public MyArrayList8() { front=null; back=null; mySize=0; } public E get(int pos) { Cell ptr=front; if(pos>=mySize) throw new IndexOutOfBoundsException(); for(int count=0; count ptr=front; if(pos>=mySize) throw new IndexOutOfBoundsException(); for(int count=0; count(item,null,null); back = front; } else { back.next = new Cell(item,null,back); back = back.next; } mySize=mySize+1; } public void add(int pos,E item) { if(pos>mySize) throw new IndexOutOfBoundsException(); if(pos==0) { front = new Cell(item,front,null); if(mySize==0) back=front; else front.next.prev=front; } else { Cell ptr=front; for(int count=1; count(item,ptr.next,ptr); if(ptr==back) back=ptr.next; else ptr.next.next.prev=ptr.next; } mySize=mySize+1; } public E remove(int pos) { E item; if(pos>=mySize) throw new IndexOutOfBoundsException(); else if(pos==0) { mySize=mySize-1; if(mySize==0) back=null; item=front.data; if(front.next!=null) front.next.prev=null; front=front.next; return item; } else { Cell ptr=front; for(int count=1; count ptr=front; for(; ptr.next!=null&&!item.equals(ptr.next.data); ptr=ptr.next) {} if(ptr.next==null) return false; else { mySize=mySize-1; if(ptr.next==back) { back=ptr; ptr.next=null; } else { ptr.next.next.prev=ptr; ptr.next = ptr.next.next; } return true; } } } public int size() { return mySize; } public int indexOf(E item) { int pos=0; Cell ptr=front; for(; ptr!=null&&!item.equals(ptr.data); ptr=ptr.next, pos++) {} if(ptr==null) return -1; else return pos; } public int lastIndexOf(E item) { int pos=mySize-1; Cell ptr=back; for(; ptr!=null&&!item.equals(ptr.data); ptr=ptr.prev, pos--) {} return pos; } public String toString() { String str="["; if(front!=null) { str+=front.data; for(Cell ptr=front.next; ptr!=null; ptr=ptr.next) str+=","+ptr.data; } return str+"]"; } private static class Cell { T data; Cell next,prev; Cell(T d,Cell n,Cell p) { data=d; next=n; prev=p; } } }