I have a simple linked list. The node contains a string (value) and an int (count).
In the linkedlist when I insert I need to insert the new Node in alphabetical order. If there is a node with the same value in the list, then I simply increment the count of the node.
I think I got my method really screwed up.
public void addToList(Node node){
//check if list is empty, if so insert at head
if(count == 0 ){
head = node;
head.setNext(null);
count++;
}
else{
Node temp = head;
for(int i=0; i<count; i++){
//if value is greater, insert after
if(node.getItem().getValue().compareTo(temp.getItem().getValue()) > 0){
node.setNext(temp.getNext());
temp.setNext(node);
}
//if value is equal just increment the counter
else if(node.getItem().getValue().compareTo(temp.getItem().getValue()) == 0){
temp.getItem().setCount(temp.getItem().getCount() + 1);
}
//else insert before
else{
node.setNext(temp);
}
}
}
}
Ok so this is inserting all my strings, but not in alphabetical order. Do you see any error?
public Node findIsertionPoint(Node head, Node node){
if( head == null)
return null;
Node curr = head;
while( curr != null){
if( curr.getValue().compareTo(node.getValue()) == 0)
return curr;
else if( curr.getNext() == null || curr.getNext().getValue().compareTo(node.getValue()) > 0)
return curr;
else
curr = curr.getNext();
}
return null;
}
public void insert(Node node){
Node newNode = node;
Node insertPoint = this.findIsertionPoint(this.head, node);
if( insertPoint == null)
this.head = newNode;
else{
if( insertPoint.getValue().compareTo(node.getValue()) == 0)
insertPoint.getItem().incrementCount();
else{
newNode.setNext(insertPoint.getNext());
insertPoint.setNext(newNode);
}
}
count++;
}
There are a few bugs with your code:
headactually needs to happen in two different scenarios:headbecomesnodenodeis less than the first element,headalso becomesnodenodelinks to whateverheadwas pointing to before (nullor a real node), andheadnow points tonode.head, then you must be inserting after some node. We just need to find where this place is. There are two scenarios:node.getValue() > temp.getValue(), andnode.getValue() < temp.getNext().getValue()node.getValue() > temp.getValue()andtemp.getNext() == nullnodeis inserted betweentempandtemp.getNext()I suggest encapsulating the after insertion point search in its own function. That is, given the list and a value, it needs to return a node. If that node has the same value as the search value, then simply increment; otherwise, insert after. As a special case, return
nullto indicate that the insertion point is beforehead.In pseudocode, it’ll look like this:
Update: I see that you’ve translated my pseudocode to Java, but for some reason you’ve omitted codes that deals with inserting before
headwhenheadis not empty. Specifically, you have inexplicably omitted this part:and this part:
Both of these are essential; it’s what allows
"A"to be inserted before theheadin[ "B", "C", "D" ].You need to understand why they’re important, and really ask yourself why you chose to remove them. Explain to us, to me, to yourself, why you did that; realize the mistake and learn from it.