I'm working on creating a program that will take an input text file and will print out the 10 most commonly used words and how many times they are used. However, it currently prints 10 random words, not ordered. Is there anything that I am missing?
public void insert(E word) {
if (word.equals("")) {
return;
}
//Adds 2 temporary nodes, and sets first to the first one if first is empty
Node temp = new Node(word);
Node temp2;
if (first == null) {
first = temp;
} else{
for (Node temp6 = first; temp6 != null; temp6 = temp6.next) {
if (temp6.key.equals(temp.key)) {
temp6.count++;
temp2 = temp6;
Node parent = first;
Node parent2 = first;
while (parent != null) {
if (parent.key.equals(word)) {
if (parent == first) {
first = first.next;
} else {
parent2.next = parent.next;
}
}
parent2 = parent;
parent = parent.next;
}
//replaces first with temp2 if temp2's count is higher than first's
if (temp2.count > first.count) {
Node temp3 = first;
first = temp2;
first.next = temp3;
}
//Adds 1 to the counter if the word is already in the linkedlist. Moves the node to the correct place and deletes the original node.
for (Node temp4 = first.next; temp4 != null; temp4 = temp4.next){
if(temp4.next.count < first.count){
Node temp5 = temp4.next;
temp4.next = temp2;
temp2.next = temp5;
break;
}
}
return;
}
}
current.next = temp;
}
current = temp;
}