Question
Write a program that maintains the top ten scores for a game application, implementing the add and remove methods using a singly linked list instead
Write a program that maintains the top ten scores for a game application, implementing the add and remove methods using a singly linked list instead of a doubly linked list.
Please model answer after program below.
PROGRAM SHOULD USE A SINGLY LINKED LIST (NODES ONLY STORE ELEMENT AND LINK TO NEXT NODE, NOT PREVIOUS)
public class GameEntry { private String name; private int score; public GameEntry(String n, int s) { name = n; score = s; }
public String getName() { return name; }
public int getScore() { return score; }
public String toString() { return "(" + name + ", " + score + ")"; } }
public class ScoreDouble
public GameEntry getElement() { return element; }
public Node
public Node
public void setPrev(Node
public void setNext(Node
public int numEntries() { return numEntries; }
public boolean isEmpty() { return numEntries == 0; }
public Node
public Node
public void add(GameEntry e) { if (numEntries == 10 && e.getScore() < header.next.element.getScore()) { return; } if (isEmpty()) { Node
public GameEntry remove(int i) throws IndexOutOfBoundsException { if (i < 0 || i >= numEntries) { throw new IndexOutOfBoundsException("Invalid index: " + i); } int count = 0; Node current = header.next; Node previous = null; while (current != trailer && count < i) { previous = current; current = current.next; count++; } GameEntry result = null; if (i == 0) { result = header.next.element; header.next = header.next.next; } else if (i == numEntries - 1) { result = trailer.prev.element; previous.next = trailer; trailer = previous.next; } else { result = previous.next.element; previous.next = current.next; current.next.prev = previous; } numEntries--; if (numEntries == 0) trailer = null; return result; }
import java.util.*; public class Driver { public static void main(String args[]) { ScoreDouble
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started