I implement selecting sort and I need to swap elements.
I have doubly linked list with previous and next links.
And link to the first and last element in List
I always need to swap some node b
with first node toStartFrom
public void Swap(Node toStartFrom, Node b) {
Boolean NextToEachOther = (toStartFrom.next == b);
toStartFrom.next = b.next;
b.previous = toStartFrom.previous;
if (NextToEachOther) {
toStartFrom.previous = b;
b.next = toStartFrom;
} else {
toStartFrom.previous = b.previous;
b.next = toStartFrom.next;
}
}
public void display() {
Node current = first;
while (current != null) {
...printing...
current = current.next;
}
}
But it doesn't work.
No errors just doesn't sort in right order.
And not diplays any elements after sort after toStartFrom
node.