// List.java public class List { private ListNode firstNode; private ListNode lastNode; private int size = 0; private String name; // constructor creates empty list with name public List() { this( "list" ); } // end constructor // constructor creates empty list with a name public List( String listName ) { firstNode = lastNode = null; name = listName; } // end constructor // get first node public ListNode getFirstNode() { return firstNode; } // end method getFirstNode // get last node public ListNode getLastNode() { return lastNode; } // end method getLastNode // get size public int getSize() { return size; } // end method getSize // insert a node at the front of the list public void insertAtFront( Object insertItem ) { if( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else firstNode = new ListNode( insertItem, firstNode ); size++; } // end method insertAtFront // insert a node at the back of the list public void insertAtBack( Object insertItem ) { if( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else lastNode = lastNode.nextNode = new ListNode( insertItem ); size++; } // end method insertAtBack // remove node at specified position or original list public Object removeNodeAt( int position, int removedNodes ) throws EmptyListException { ListNode current = firstNode; ListNode removed = null; if ( position > 1 && position <= size ) { for( int i = 1; i < position - 1; i++ ) current = current.nextNode; removed = current.nextNode; current.nextNode = current.nextNode.nextNode; size--; System.out.printf( "Node at %d removed\n\n", position + removedNodes ); } // end if else if ( position == 1 && position <= size) { removed = firstNode; firstNode = firstNode.nextNode; size--; System.out.printf( "Node at %d removed\n\n", position + removedNodes ); } else if ( position < 1 && position < size ) System.out.printf( "Cannot remove node at %d, does not exist\n\n", position + removedNodes ); else if ( isEmpty() ) System.out.printf( "No elements to be removed.\n\n" ); else if ( position > size ) System.out.printf( "Cannot remove node at %d, does not exist.\n\n", position + removedNodes ); return removed; } // end method removeNodeAt public boolean isEmpty() { return firstNode == null; } // end method isEmpty // output list contents public void print() { if( isEmpty() ) { System.out.printf( "Empty %s\n", name ); return; } // end if System.out.printf( "The list %s is: ", name ); ListNode current = firstNode; // while not at the end of list output current node's data while( current != null ) { System.out.printf( "%s ", current.data ); current = current.nextNode; } // end while System.out.println(); System.out.printf ( "The size of list is %d\n", size ); System.out.println( "\n" ); } // end method print } // end class List