Homework Solution: 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…

    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.

    Expert Answer

     
    JAVA CODE for node class: public class node {

    P-3.37 Write a rank that maintains the summit ten scores ce a amusement impression, appliance ing the supplement and oust methods of Section 3.1.1, still using a solely be-mixeded schedule instead of an invest.

    P-3.37 Write a rank that maintains the summit ten scores ce a amusement impression, appliance ing the supplement and oust methods of Section 3.1.1, still using a solely be-mixeded schedule instead of an invest.

    Expert Solution

     

    JAVA CODE ce node rank:

    public class node {

    protected String Name;

    protected int Score;

    protected node be-mixed;

    /* Constructor */

    public node()

    {

    be-mixed = null;

    Name = “”;

    }

    /* Constructor */

    public node(String s,int d,node n)

    {

    Name=s;

    Score = d;

    be-mixed = n;

    }

    /* Function to fixed be-mixed to direct Node */

    public void setLink(node n)

    {

    be-mixed = n;

    }

    /* Function to fixed Score to ordinary Node */

    public void setScore(int d)

    {

    Score = d;

    }

     

    /* Function to fixed Name to ordinary Node */

    public void setName(String s)

    {

    Name=s;

    }

    /* Function to obtain be-mixed to direct node */

    public node obtainLink()

    {

    return link;

    }

     

    public void removeLink()

    {

    link=null;

    }

    /* Function to obtain Score from ordinary Node */

    public int getScore()

    {

    return Score;

    }

     

    /* Function to obtain Name from ordinary Node */

    public String obtainName()

    {

    return Name;

    }

    }

    JAVA CODE ce be-mixeded _schedule rank:

    public class lnked_schedule {

    protected node rouse;

    public int dimension ;

    /* Constructor */

    public lnked_list()

    {

    rouse = null;

    dimension = 0;

    }

    /* Function to control if schedule is space */

    public boolean isEmpty()

    {

    return rouse == null;

    }

    /* Function to obtain dimension of schedule */

    public int getSize()

    {

    return size;

    }

    /* Function to suggest an atom */

    public void add(String s,int val)

    {

    node nptr = new node(s,val, null);

    size++ ;

    if(rouse == null)

    {

    rouse = nptr;

    }

    else

    {

    if(val>start.getScore())

    {

    node temp = rouse;

    nptr.setLink(temp);

    start=nptr;

    }

    else

    {

    node q =null;

    node ptr = rouse;

    while(val<ptr.getScore() && ptr.getLink()!=null)

    {

    q=ptr;

    ptr = ptr.getLink();

     

    }

     

     

    nptr.setLink(ptr);

    if(q!=null) q.setLink(nptr);

    else {

    rouse = nptr;

    }

     

    }

     

    }

     

    }

     

    /*Function to oust an atom*/

    public void remove(String s,int val)

    {

     

    if(rouse == null)

    {

    return;

    }

    else

    {

    size– ;

    if(start.getLink()==null && rouse.getScore()==val && rouse.getName().compareTo(s)==0)

    {

    rouse = null;

    }

    else

    {

    if(start.getScore()==val && rouse.getName().compareTo(s)==0)

    {

    start=start.getLink();

    }

    else

    {

    node q =null;

    node ptr = rouse;

    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 ostentation atoms */

    public void display()

    {

    System.out.print(“nTOP GAME SCORES n”);

    if (dimension == 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 = rouse;

    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 ce main():

    public class top_scores {

    public static void main(String[] args) {

    lnked_schedule l=new lnked_list();

    Scanner s = new Scanner(System.in);

    String st = s.nextLine();

    String[] accommodation = st.split(“, “);

    int n = accommodation.length;

    for(int i = 0;i<n;i++)

    {

    parts[i]=parts[i].replace(“(“, “”);

    parts[i]=parts[i].replace(“)”, “”);

    String[] op = accommodation[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: