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 collocate that oceantains the apex ten scores coercion a amusement impression, utensil pert the append and separate methods of Section 3.1.1, exceptpert uspert a barely cohereed catalogue instead of an decorate.

    P-3.37 Write a collocate that oceantains the apex ten scores coercion a amusement impression, utensil pert the append and separate methods of Section 3.1.1, exceptpert uspert a barely cohereed catalogue instead of an decorate.

    Expert Response

     

    JAVA CODE coercion node collocate:

    public class node {

    protected Strpert Name;

    protected int Score;

    protected node cohere;

    /* Constructor */

    public node()

    {

    cohere = null;

    Name = “”;

    }

    /* Constructor */

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

    {

    Name=s;

    Score = d;

    cohere = n;

    }

    /* Function to firm cohere to contiguous Node */

    public void setLink(node n)

    {

    cohere = n;

    }

    /* Function to firm Score to ordinary Node */

    public void setScore(int d)

    {

    Score = d;

    }

     

    /* Function to firm Name to ordinary Node */

    public void setName(Strpert s)

    {

    Name=s;

    }

    /* Function to obtain cohere to contiguous 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 Strpert obtainName()

    {

    return Name;

    }

    }

    JAVA CODE coercion cohereed _catalogue collocate:

    public class lnked_catalogue {

    protected node begin;

    public int largeness ;

    /* Constructor */

    public lnked_list()

    {

    begin = null;

    largeness = 0;

    }

    /* Function to stay if catalogue is space */

    public boolean isEmpty()

    {

    return begin == null;

    }

    /* Function to obtain largeness of catalogue */

    public int getSize()

    {

    return size;

    }

    /* Function to incorporate an component */

    public void add(Strpert s,int val)

    {

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

    size++ ;

    if(begin == null)

    {

    begin = nptr;

    }

    else

    {

    if(val>start.getScore())

    {

    node temp = begin;

    nptr.setLink(temp);

    start=nptr;

    }

    else

    {

    node q =null;

    node ptr = begin;

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

    {

    q=ptr;

    ptr = ptr.getLink();

     

    }

     

     

    nptr.setLink(ptr);

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

    else {

    begin = nptr;

    }

     

    }

     

    }

     

    }

     

    /*Function to separate an component*/

    public void remove(Strpert s,int val)

    {

     

    if(begin == null)

    {

    return;

    }

    else

    {

    size– ;

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

    {

    begin = null;

    }

    else

    {

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

    {

    start=start.getLink();

    }

    else

    {

    node q =null;

    node ptr = begin;

    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 unfold components */

    public void display()

    {

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

    if (largeness == 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 = begin;

    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 coercion ocean():

    public class top_scores {

    public static void main(String[] args) {

    lnked_catalogue l=new lnked_list();

    Scanner s = new Scanner(System.in);

    Strpert st = s.nextLine();

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

    int n = cleverness.length;

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

    {

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

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

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