I have a problem in understanding the algorithm of the Linked List, if someone could explain for me, I understood almost everything only a specific part, when the new added item is less than the previous item so we have to move the new item to the left
public boolean addItem(ListItem newItem) {
if (this.root == null) {
// The list was empty, so this item becomes the head of the list
this.root = newItem;
return true;
}
ListItem currentItem = this.root;
while (currentItem != null) {
int comparison = (currentItem.compareTo(newItem));
if (comparison < 0) {
// newItem is greater, move right if possible
if (currentItem.next() != null) {
currentItem = currentItem.next();
} else {
// there is no next, so insert at end of list
currentItem.setNext(newItem).setPrevious(currentItem);
return true;
}
} else if (comparison > 0) {
// newItem is less, insert before
if (currentItem.previous() != null) {
currentItem.previous().setNext(newItem);//previous entry is now the added item
newItem.setPrevious(currentItem.previous()); //the new item previous entry is setted to the current item previous value
newItem.setNext(currentItem); //pointing the new item to the current item
currentItem.setPrevious(newItem); //the previous entry of the current item is equal with the new item
} else {
// the node with a previous is the root
newItem.setNext(this.root).setPrevious(newItem);
this.root = newItem;
}
return true;
} else {
// equal
System.out.println(newItem.getValue() + " is already present, not added.");
return false;
}
}
return false;
}
so where comparison is greater than 0, so lets say I have: 9 11 10 in my Linked List, so the previous entry of 10, which is 11 goes to the previous position to the right, newItem(10)is setted to the previous position with the currentItem.previous value, so the current item is not also 10 now ?