Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN JAVA, PLEASE WRITE A CODE USING ECLIPSE AND THE FOLLOWING CODE BELOW, Also please show the output Write a program that maintains the top
IN JAVA, PLEASE WRITE A CODE USING ECLIPSE AND THE FOLLOWING CODE BELOW, Also please show the output Write a program that maintains the top scores for the students of a class that has more than students implementing the add and remove methods using a doubly linked list. Moreover, your implementation of removei should make the fewest number of pointer hops to get to the student at index i class DoublyNode
Student data;
DoublyNode next;
DoublyNode prev;
public DoublyNodeStudent data
this.data data;
this.next null;
this.prev null;
class TopScoresDoublyList
private DoublyNode head;
private int size;
public TopScoresDoublyList
this.head null;
this.size ;
public void addStudent student
DoublyNode newNode new DoublyNodestudent;
if head null student.score head.data.score
newNode.next head;
if head null
head.prev newNode;
head newNode;
else
DoublyNode current head;
while currentnext null && student.score current.next.data.score
current current.next;
newNode.next current.next;
newNode.prev current;
if currentnext null
current.next.prev newNode;
current.next newNode;
size;
Keep only the top scores
if size
remove;
public void removeint index
if index index size
System.out.printlnInvalid index to remove.";
return;
DoublyNode current head;
for int i ; i index; i
current current.next;
if currentprev null
current.prev.next current.next;
else
head current.next;
if currentnext null
current.next.prev current.prev;
size;
public void display
DoublyNode current head;
while current null
System.out.printlncurrentdata.name : current.data.score;
current current.next;
public class Main
public static void mainString args
TopScoresDoublyList scoresList new TopScoresDoublyList;
Adding students
scoresList.addnew StudentJohn;
scoresList.addnew StudentAlice;
scoresList.addnew StudentBob;
scoresList.addnew StudentCharlie;
scoresList.addnew StudentDavid;
scoresList.addnew StudentEmma;
scoresList.addnew StudentFrank;
scoresList.addnew StudentGrace;
scoresList.addnew StudentHenry;
scoresList.addnew StudentIvy;
scoresList.addnew StudentJack;
scoresList.addnew StudentKelly;
scoresList.addnew StudentLiam;
scoresList.addnew StudentMia;
scoresList.addnew StudentNoah;
scoresList.addnew StudentOlivia;
scoresList.addnew StudentSophia;
System.out.printlnTop Scores:";
scoresList.display;
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