public void addElement ( T data ) { Node < T > newNode = new Node < T > ( data ) ; if ( isEmpty ( ) ) { // // if it is the very first node in the list first = newNode ; size = 1 ; } else { // if this node is greater than the list's head // if ( this . comparator . compare ( newNode . data , first . data ) > 0 ) { Node < T > tmp = first ; first = newNode ; first . next = tmp ; tmp . prev = first ; size ++ ; } else { Node < T > prev = first ; Node < T > current = first . next ; while ( current != null ) { // if this node is the current node less than the new one // if ( this . comparator . compare ( newNode . data , current . data ) > 0 ) { prev . next = newNode ; newNode . prev = prev ; current . prev = newNode ; newNode . next = current ; size ++ ; break ; } current = current . next ; prev = prev . next ; } // if all list elements are greater than the new one // if ( null == current ) { prev . next = newNode ; newNode . prev = prev ; size ++ ; } if ( size > this . maxSize ) { dropLastElement ( ) ; } } } }
Adds a data instance .