Question: Use this code, but use a doubly linked list instead of a singly linked list. Moreover, your implementation of remove( i ) should make the

Use this code, but use a doubly linked list instead of a singly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at index i.

"Do you want a custom designed DoublyLinkedList or do you want the java provided LinkedLIst class to be used with doubly linked list related methods?"

I want to the java provided HighScores class to instead be designed as a DoublyLinkedList.

import java.util.LinkedList; import java.util.Scanner; public class HighScore { private LinkedList < String > scores; public static void main(String[] args) { HighScore highScore = new HighScore(); Scanner scanner = new Scanner(System.in); while (true) { try { System.out.println("Enter the name (type 'stop' to quit):"); String name = scanner.nextLine(); if ("stop".equals(name)) { break; } System.out.println("Enter the high score:"); String score = scanner.nextLine(); highScore.insert(name, Integer.parseInt(score)); System.out.println("High scores:"); System.out.println(highScore.toString()); } catch (Exception e) { System.out.println("Invalid input, try again: " + e); e.printStackTrace(); } } } // constructor public HighScore() { // create an empty ArrayList scores = new LinkedList < String > (); } public void insert(String name, Integer score) { String newScore = name + " " + score.toString(); if (scores.isEmpty()) { scores.add(newScore); return; } for (int i = 0; i <= scores.size(); i++) { if (i == scores.size()) { scores.add(newScore); break; } if (isGreaterThan(newScore, scores.get(i))) { scores.add(i, newScore); break; } } // shrink list to 10 members while (scores.size() > 10) { scores.remove(10); } }

// decide whether the new score is greater than the one we are looking at public boolean isGreaterThan(String first, String second) { Integer firstScore = Integer.parseInt(first.substring(first.lastIndexOf(' ') + 1)); Integer secondScore = Integer.parseInt(second.substring(second.lastIndexOf(' ') + 1)); return firstScore > secondScore; } @Override public String toString() { String scoreList = ""; for (int i = 0; i < scores.size(); i++) { scoreList = scoreList + scores.get(i) + " "; } return scoreList; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!