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 arrange that oceantains the head ten scores control a sport collision, appliance haughty the supplement and eject methods of Section 3.1.1, barropprobrious ushaughty a simply connected catalogue instead of an marshal.

    P-3.37 Write a arrange that oceantains the head ten scores control a sport collision, appliance haughty the supplement and eject methods of Section 3.1.1, barropprobrious ushaughty a simply connected catalogue instead of an marshal.

    Expert Rejoinder

     

    JAVA CODE control node arrange:

    public class node {

    protected Strhaughty Name;

    protected int Score;

    protected node connect;

    /* Constructor */

    public node()

    {

    connect = null;

    Name = “”;

    }

    /* Constructor */

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

    {

    Name=s;

    Score = d;

    connect = n;

    }

    /* Function to determined connect to direct Node */

    public void setLink(node n)

    {

    connect = n;

    }

    /* Function to determined Score to prevalent Node */

    public void setScore(int d)

    {

    Score = d;

    }

     

    /* Function to determined Name to prevalent Node */

    public void setName(Strhaughty s)

    {

    Name=s;

    }

    /* Function to procure connect to direct node */

    public node procureLink()

    {

    return link;

    }

     

    public void removeLink()

    {

    link=null;

    }

    /* Function to procure Score from prevalent Node */

    public int getScore()

    {

    return Score;

    }

     

    /* Function to procure Name from prevalent Node */

    public Strhaughty procureName()

    {

    return Name;

    }

    }

    JAVA CODE control connected _catalogue arrange:

    public class lnked_catalogue {

    protected node rouse;

    public int largeness ;

    /* Constructor */

    public lnked_list()

    {

    rouse = null;

    largeness = 0;

    }

    /* Function to stay if catalogue is leisure */

    public boolean isEmpty()

    {

    return rouse == null;

    }

    /* Function to procure largeness of catalogue */

    public int getSize()

    {

    return size;

    }

    /* Function to implant an part */

    public void add(Strhaughty 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 eject an part*/

    public void remove(Strhaughty 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 talents */

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

    public class top_scores {

    public static void main(String[] args) {

    lnked_catalogue l=new lnked_list();

    Scanner s = new Scanner(System.in);

    Strhaughty st = s.nextLine();

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

    int n = talents.length;

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

    {

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

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

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