public void addToPosition (int position) { Node anotherNode = new Node (currentDataValue); Node temp; Node prior; Node after; int index; if ((position < 1) || (position > (length+1))) { System.out.println("Position must be a value between 1-" + (length+1)); } else { // List is empty if (head == null) { if (position == 1) { currentDataValue += 10; length++; head = anotherNode; } else System.out.println("List empty, unable to add node to " + "position " + position); } // List is not empty, inserting into first position. else if (position == 1) { head.previous = anotherNode; anotherNode.next = head; head = anotherNode; currentDataValue += 10; length++; } // List is not empty inserting into a position other than the first else { prior = head; index = 1; // Traverse list until current is referring to the node in front // of the position that we wish to insert the new node into. while (index < (position-1)) { prior = prior.next; index++; } after = prior.next; // Set the references to the node before the node to be // inserted. prior.next = anotherNode; anotherNode.previous = prior; // Set the references to the node after the node to be // inserted. if (after != null) after.previous = anotherNode; anotherNode.next = after; currentDataValue += 10; length++; } } }