Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 static class Node { private GameEntry element; private Node prev; private Node next; public Node(GameEntry e, Node p, Node n) { element = e; prev = p; next = n; }

public GameEntry getElement() { return element; }

public Node getPrev() { return prev; }

public Node getNext() { return next; }

public void setPrev(Node p) { prev = p; }

public void setNext(Node n) { next = n; } } private Node header; private Node trailer; private int numEntries = 0; public ScoreDouble() { header = new Node (null, null, null); trailer = new Node(null, header, null); header.setNext(trailer); }

public int numEntries() { return numEntries; }

public boolean isEmpty() { return numEntries == 0; }

public Node getHeader() { return header; }

public Node getTrailer() { return trailer; }

public void add(GameEntry e) { if (numEntries == 10 && e.getScore() < header.next.element.getScore()) { return; } if (isEmpty()) { Node newest = new Node<>(e, header, trailer); header.next = newest; trailer.prev = newest; } else if (e.getScore() < header.next.element.getScore()) { Node newest = new Node<>(e, header, header.next); header.next = newest; header.next.prev = newest; } else if (e.getScore() > trailer.prev.element.getScore()) { Node newest = new Node<>(e, trailer.prev, trailer); trailer.prev.next = newest; trailer.prev = newest; } else { Node current = header.next; Node previous = null; while(current != trailer && e.getScore() > current.element.getScore()) { previous = current; current = current.next; } Node newest = new Node<>(e, previous, previous.next); previous.next = newest; previous.next.prev = newest; } numEntries++; if (numEntries > 10) { header.next = header.next.next; numEntries--; } }

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 topTen = new ScoreDouble<>(); String[] names = {"Shane", "Garret", "Beau", "Luke", "Sam", "Renee", "Larry", "Jean", "Jack", "Ethan", "Divante", "Jake"}; int[] scores = {700, 250, 860, 750, 1000, 420, 710, 900, 610, 730, 970, 390}; for (int i = 0; i < names.length; i++) { GameEntry e = new GameEntry(names[i], scores[i]); System.out.println("Adding " + e); topTen.add(e); } System.out.println(" Top ten scores:"); ScoreDouble.Node node = topTen.getHeader(); while (node != null) { System.out.printf("%s ", node.getElement()); node = node.getNext(); } System.out.println(" Removing " + topTen.remove(3)); System.out.println(" Top ten scores:"); node = topTen.getHeader(); while (node != null) { System.out.printf("%s ", node.getElement()); node = node.getNext(); } GameEntry eNew = new GameEntry("Chris", 950); System.out.println(" Adding " + eNew); topTen.add(eNew); System.out.println(" Top ten scores:"); node = topTen.getHeader(); while (node != null) { System.out.printf("%s ", node.getElement()); node = node.getNext(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

Students also viewed these Databases questions

Question

=+2 Is the decision sustainable in the long run?

Answered: 1 week ago

Question

=+1 Is the decision fair to employees?

Answered: 1 week ago