A game is played in my code, but first a file is read and the leaderboard is kept in a linkedlist. After playing the game, a score is obtained and this score is added in the order of the linked list. All I have to do is write this new list to the txt file.
Here is the list before playing the game .
Pelin;30
Kaan;15
Ali;50
Yeliz;25
Cem;40
Can;35
Ece;5
Sibel;30
Remzi;20
Nazan;10
Here is my linked list node class:
public class Node {
private Object data;
private Node link;
public Node(Object dataToAdd) {
data = dataToAdd;
link = null;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLink() {
return link;
}
public void setLink(Node link) {
this.link = link;
}
}
And here is my SLL class:
public class SingleLinkedList {
Node head;
public void insertScore(Object dataToAdd) {
Node newNode = new Node(dataToAdd);
if (head == null) {
head = newNode;
} else {
int score = getScore(dataToAdd);
int headObjectScore = getScore(head.getData());
if (score > headObjectScore) {
Node temp = head;
head = newNode;
newNode.setLink(temp);
} else {
Node currentNode = head;
// to handle if the newNode has the biggest score
if (currentNode.getLink() == null) {
currentNode.setLink(newNode);
currentNode = newNode;
}
while (currentNode.getLink() != null) {
Node oldNode = currentNode;
currentNode = currentNode.getLink();
if (score > getScore(currentNode.getData())) {
oldNode.setLink(newNode);
newNode.setLink(currentNode);
break;
}
// to handle if the newNode has the biggest score
if (currentNode.getLink() == null) {
currentNode.setLink(newNode);
currentNode = newNode;
}
}
}
}
}
private int getScore(Object data) {
return Integer.valueOf(((String) data).split(";")[1]);
}
I need to print the list txt file
I have completed to manage high score table but no idea to how can i change .txt file with the new linkedlist