Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use this code, but use a doubly linked list instead of a singly linked list. Mor

ID: 3863325 • Letter: U

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 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;
}
}

Explanation / Answer

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);
       boolean stop = false;
       while (!stop) {
           try {
               System.out.println("[1]Add ");
               System.out.println("[2]Remove ");
               System.out.println("[3]Print ");
               System.out.println("[4]Stop ");
               System.out.println("Enter your choice: ");
               int choice = scanner.nextInt();
               switch(choice){
               case 1:
                   System.out.println("Enter the name :");
                   String name = scanner.next();
                   System.out.println("Enter the high score:");
                   String score = scanner.next();
                   highScore.insert(name, Integer.parseInt(score));
                   System.out.println("Added Successfully !");
                   break;
               case 2:
                   System.out.println("Enter Position to remove :");
                   int pos = scanner.nextInt();
                   highScore.remove(pos);
                   break;
               case 3:
                   System.out.println("High Scores:");
                   System.out.println(highScore.toString());
                   break;
               case 4:
                   stop = true;
                   break;
               default :
                   System.out.println("Invalid input, try again");
                   break;
               }
           } catch (Exception e) {
               System.out.println("Invalid input, try again: " + e);
               e.printStackTrace();
           }
       }
       System.out.println("Stopped Successfully !");
       scanner.close();
   }
   // 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);
       }
   }
  
   public void remove(int index) {
       if(index >= scores.size()){
           System.out.println("Index is out of size");
           return;
       }
       scores.remove(index);
       System.out.println("Removed Successfully");
   }
  
   // 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;
   }
}

//============================ OUTPUT ===========================//

[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
1
Enter the name :
e
Enter the high score:
3
Added Successfully !
[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
3
High Scores:
e 3

[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
1
Enter the name :
dd
Enter the high score:
33
Added Successfully !
[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
3
High Scores:
dd 33
e 3

[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
2
Enter Position to remove :
1
Removed Successfully
[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
3
High Scores:
dd 33

[1]Add
[2]Remove
[3]Print
[4]Stop
Enter your choice:
4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote