P-3.37 Write a class that maintains the top ten scores for a game application, implement ing the add and remove methods of Section 3.1.1, but using a singly linked list instead of an array.
P-3.37 Write a class that maintains the top ten scores for a game application, implement ing the add and remove methods of Section 3.1.1, but using a singly linked list instead of an array.
JAVA CODE for node class:
public class node {
protected String Name;
protected int Score;
protected node link;
/* Constructor */
public node()
{
link = null;
Name = “”;
}
/* Constructor */
public node(String s,int d,node n)
{
Name=s;
Score = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(node n)
{
link = n;
}
/* Function to set Score to current Node */
public void setScore(int d)
{
Score = d;
}
/* Function to set Name to current Node */
public void setName(String s)
{
Name=s;
}
/* Function to get link to next node */
public node getLink()
{
return link;
}
public void removeLink()
{
link=null;
}
/* Function to get Score from current Node */
public int getScore()
{
return Score;
}
/* Function to get Name from current Node */
public String getName()
{
return Name;
}
}
JAVA CODE for linked _list class:
public class lnked_list {
protected node start;
public int size ;
/* Constructor */
public lnked_list()
{
start = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element */
public void add(String s,int val)
{
node nptr = new node(s,val, null);
size++ ;
if(start == null)
{
start = nptr;
}
else
{
if(val>start.getScore())
{
node temp = start;
nptr.setLink(temp);
start=nptr;
}
else
{
node q =null;
node ptr = start;
while(val<ptr.getScore() && ptr.getLink()!=null)
{
q=ptr;
ptr = ptr.getLink();
}
nptr.setLink(ptr);
if(q!=null) q.setLink(nptr);
else {
start = nptr;
}
}
}
}
/*Function to remove an element*/
public void remove(String s,int val)
{
if(start == null)
{
return;
}
else
{
size– ;
if(start.getLink()==null && start.getScore()==val && start.getName().compareTo(s)==0)
{
start = null;
}
else
{
if(start.getScore()==val && start.getName().compareTo(s)==0)
{
start=start.getLink();
}
else
{
node q =null;
node ptr = start;
while(val<ptr.getScore() && ptr.getLink()!=null)
{
q = ptr;
ptr = ptr.getLink();
}
if(ptr.getScore()==val && ptr.getName().compareTo(s)==0)
{
q.setLink(ptr.getLink());
}
else
{
return;
}
}
}
}
}
/* Function to display elements */
public void display()
{
System.out.print(“nTOP GAME SCORES n”);
if (size == 0)
{
System.out.print(“emptyn”);
return;
}
int cnt=1;
if (start.getLink() == null)
{
System.out.println(cnt+”t”+start.getName()+”t”+start.getScore());
return;
}
node ptr = start;
while (ptr.getLink() != null && cnt<10)
{
System.out.println(cnt+”t”+ptr.getName()+”t”+ptr.getScore());
ptr = ptr.getLink();
cnt++;
}
System.out.println(cnt+”t”+ptr.getName()+”t”+ptr.getScore());
}
}
JAVA CODE for main():
public class top_scores {
public static void main(String[] args) {
lnked_list l=new lnked_list();
Scanner s = new Scanner(System.in);
String st = s.nextLine();
String[] parts = st.split(“, “);
int n = parts.length;
for(int i = 0;i<n;i++)
{
parts[i]=parts[i].replace(“(“, “”);
parts[i]=parts[i].replace(“)”, “”);
String[] op = parts[i].split(“,”);
if(op[0].compareTo(“A”)==0)
{
l.add(op[1], Integer.parseInt(op[2]));
}
else
{
l.remove(op[1], Integer.parseInt(op[2]));
}
}
l.display();
s.close();
}
}
OUTPUT:
Help Me Write My Essay - Reasons:
Best Online Essay Writing Service
We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.
Our Writers are Experienced and Professional
Our essay writing service is founded on professional writers who are on stand by to help you any time.
Free Revision Fo all Essays
Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.
Timely Essay(s)
We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.
Customised Essays &100% Confidential
Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.
24/7 Customer Support
Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
HOW OUR ONLINE ESSAY WRITING SERVICE WORKS
Let us write that nagging essay.
STEP 1
By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.
STEP 2
Pick A & Writer
Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.
STEP 3
Editing (OUR PART)
At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.
STEP 4
Receive your Paper
After Editing, your paper will be sent to you via email.