import java.util.Arrays; public class IntegerLinkedList implements IntegerList { private int elements; private Node head; /* Create list with no elements */ public IntegerLinkedList(){ elements = 0; this.head = null; } /* Create list with all elements of initList in the order 0...n-1 */ public IntegerLinkedList( int[] initList ){ this(); for( int i : initList ){ this.add( i ); } } /* Create list with all the elements of otherList in the order 0...n-1 */ public IntegerLinkedList( IntegerList otherList ){ this(); for( int i = 0; i < otherList.size(); ++i ){ this.add( otherList.get( i )); } } /********* Private Methods *********/ /* Returns node object at an index. * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ private Node getNode( int index ){ if( index < 0 ) throw new IndexOutOfBoundsException("index < 0 access: [" +index +"]"); else if( index >= this.size() ) throw new IndexOutOfBoundsException("index >= size() access [" +index +"]"); Node iter = this.head; for( int i = 0; i < index; ++i ){ iter = iter.getRight(); } return iter; } /********* Public Methods *********/ /** Accessors/Getters **/ /* Returns integer at the index * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ public int get(int index) { return getNode( index ).getNumber(); } /* Returns the number of elements in the list */ public int size() { return this.elements; } /* Returns a copy of all the elements in the array; size = size() */ public int capacity() { return Integer.MAX_VALUE; } /* Returns true if there are 0 elements in the list */ public boolean isEmpty(){ return this.size() == 0; } /* Returns a copy of all the elements in the array; size = size() */ public int[] toArray() { int[] copyArray = new int[ this.size() ]; for( int i = 0; i < this.size(); ++i ){ copyArray[i] = this.get( i ); } return copyArray; } /* Returns a String representation of the elements in the array; * (non-Javadoc) * @see java.lang.Object#toString() */ public String toString(){ return Arrays.toString( this.toArray() ); } /** Modifiers/Setters **/ /* Insert integer into array at index * If index is within existing array, [0-size), performs left shift then insert * If index is == size, inserts only * if index is > size throws */ public void insert(int index, int integer) { if( index < 0 ) throw new IndexOutOfBoundsException("index < 0 access: [" +index +"]"); else if( index > this.size() ) throw new IndexOutOfBoundsException("index > size() access [" +index +"]"); if( this.size() == 0 ){ // empty list operation if( index != 0) throw new IndexOutOfBoundsException("Attempting to assign to index [" + index +"] in empty list"); this.head = new Node( integer, null, null ); } else { // nonempty list operation if( index == 0 ){ this.head = this.head.insertLeft( integer ); } else { Node modNode = this.getNode(index-1); modNode.insertRight( integer ); } } this.elements += 1; } /* Appends integer onto the list * Returns the index integer was inserted to */ public int add(int integer) { this.insert( this.size(), integer ); return this.size()-1; } /* Appends list of integers onto the list * Returns the last index inserted into */ public int add(int[] listOfInteger) { for( int i : listOfInteger ){ this.add( i ); } return this.size()-1; } /* Removes element at index from the array * Returns the element removed * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ public int remove(int index) { Node removeNode = this.getNode( index ); this.elements -= 1; if( removeNode == this.head ) this.head = removeNode.getRight(); return removeNode.remove(); } /* Sets value of element at index * Returns the previous value of the element at index * Throws IndexOutOfBoundsException if index is not w/i bounds [0...size) */ public int set(int index, int integer ) { Node modifyNode = this.getNode( index ); int previous = modifyNode.getNumber(); modifyNode.setNumber( integer ); return previous; } }