public void swap ( Element < T > a , Element < T > b ) { if ( a . next == b ) { if ( a . previous != null ) { a . previous . next = b ; } if ( b . next != null ) { b . next . previous = a ; } Element < T > tmp = a . previous ; a . previous = b ; a . next = b . next ; b . previous = tmp ; b . next = a ; if ( first == a ) first = b ; if ( last == b ) last = a ; } else if ( a . previous == b ) { if ( a . next != null ) { a . next . previous = b ; } if ( b . previous != null ) { b . previous . next = a ; } Element < T > tmp = a . next ; a . next = b ; a . previous = b . previous ; b . previous = a ; b . next = tmp ; if ( first == b ) first = a ; if ( last == a ) last = b ; } else { if ( a . next != null ) { a . next . previous = b ; } if ( a . previous != null ) { a . previous . next = b ; } if ( b . next != null ) { b . next . previous = a ; } if ( b . previous != null ) { b . previous . next = a ; } Element < T > tempNext = b . next ; Element < T > tempPrev = b . previous ; b . next = a . next ; b . previous = a . previous ; a . next = tempNext ; a . previous = tempPrev ; if ( a . next == null ) last = a ; else if ( b . next == null ) last = b ; if ( a . previous == null ) first = a ; else if ( b . previous == null ) first = b ; } }
Swaps the location of the two elements