/**
 * A class that extends MyLinkedList to keep track and use a reference to the middle.
 *   @author Dave Reed
 *   @version 10/20/17
 */
public class MidLinkedList<E> extends MyLinkedList<E> {
    private DNode<E> middle;
    
    /**
     * Constructs an empty list (with dummy nodes at each end).
     */
    public MidLinkedList() {
        super();
        this.middle = this.front;
    }
    
    /**
     * Adds an item at the specified index.
     *   @param index the location where the item is to be added
     *   @param newItem the item being added
     */
    public void add(int index, E newItem) {        
        super.add(index, newItem);
        
        int midIndex = this.size()/2;        
        if (this.size() > 1 && index < midIndex) {
            if (this.size() % 2 == 0) {
                this.middle = this.middle.getPrevious();
            }
        }
        else if (this.size() % 2 == 1) {
            this.middle = this.middle.getNext();
        }       
        // System.out.println(this.middle.getData() + " " + this.toString());
    }
}
