Question

In: Computer Science

B has to be matched with A so please I need both in Java. the previous...

B has to be matched with A so please I need both in Java.

the previous project mean A

A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array.

B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at index i.

Solutions

Expert Solution

/***************** ScoreSingly.java *****************/

public class ScoreSingly {
   Score head;
   Score tail;

   class Score {
       int data;
       Score next;

       Score(int data) {
           this.data = data;
           this.next = null;
       }
   }

   ScoreSingle() {
       head = null;
       tail = null;
   }

   public void add(int data) {
       Score newScore = new Score(data);
       if (head == null) {
           head = newScore;
           tail = newScore;
       } else {
           tail.next = newScore;
           tail = newScore;
       }
   }

   void remove(int position) {

       if (head == null)
           return;
       Score temp = head;
       if (position == 0) {
           head = temp.next;
           return;
       }
       for (int i = 0; temp != null && i < position - 1; i++)
           temp = temp.next;
       if (temp == null || temp.next == null)
           return;
       Score next = temp.next.next;
       temp.next = next;

   }

   public void display() {
       Score current = head;
       if (head == null) {
           System.out.println("List is empty");
           return;
       }
       System.out.println("Nodes of singly linked list: ");
       while (current != null) {
           System.out.print(current.data + " ");
           current = current.next;
       }
       System.out.println();
   }

}

/*************** ScoreDoubly.java *************/

public class ScoreDoubly {
   int ncount;
   Score head;
   Score tail;

   class Score {
       int item;
       Score previous;
       Score next;

       public Score(int item) {
           this.item = item;
       }
   }

   ScoreDouble() {
       ncount = 0;
       head = null;
       tail = null;
   }

   public void add(int item) {
       Score newScore = new Score(item);
       if (head == null) {
           head = tail = newScore;
           head.previous = null;
           tail.next = null;
       } else {
           tail.next = newScore;
           newScore.previous = tail;
           tail = newScore;
           tail.next = null;
       }
       ncount++;
   }

   void remove(int position) {
       if (head == null) {
           return;
       }
       if (ncount - position > position) {
           Score temp = head;
           if (position == 0) {
               head = temp.next;
               ncount--;
               return;
           }
           for (int i = 0; temp != null && i < position - 1; i++) {
               temp = temp.next;
           }
           if (temp == null || temp.next == null)
               return;
           Score next = temp.next.next;
           temp.next = next;
           ncount--;
       } else {
           if (position == ncount - 1) {
               tail = tail.previous;
               tail.next = null;
               ncount--;
               return;
           }
           Score temp = tail;
           for (int i = 0; i < position - 1 && temp != null; i++) {
               temp = temp.previous;
           }
           temp.next.previous = temp.previous;
           temp.previous.next = temp.next;
           ncount--;
       }
   }

   public void display() {
       Score current = head;
       if (head == null) {
           System.out.println("No Scores added yet.");
           return;
       }
       System.out.println("\n Scores are : ");
       while (current != null) {
           System.out.print(current.item + " ");
           current = current.next;
       }
   }
}


Related Solutions

I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B...
I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B 0 -$5,000,000 -5,000,000 1 $1,500,000 $1,250,000 2 $1,500,000 $1,250,000 3 $1,500,000 $1,250,000 4 $1,500,000 $1,250,000 5 $1,500,000 $1,250,000 6 $1,500,000 $1,250,000 7 $2,000,000 $1,250,000 8 0 $1,600,000 Calculate Payback period, NPV, IRR and profitability Index for the two projects Part (II) You have been hired as a consultant for Pristine Urban-Tech Zither, Inc. (PUTZ), manufacturers of fine zithers. The market for zithers is growing...
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
I need this in java on textpad. There are two files, both have instructions in them...
I need this in java on textpad. There are two files, both have instructions in them on what to add in the code. They are posted below. public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums =...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
please I need to correct this essay in both grammar , structur and vocab. it is...
please I need to correct this essay in both grammar , structur and vocab. it is about the connecting between CTE and STEAM. Most countries and societies tend to focus on science, technology, engineering and math education, or STEM or STEAM. Where those thought to believe that it will help improve the results of the outputs of the four disciplines: science, technology, engineering and mathematics. STEAM / STEM is interested in international organizations seeking to develop their human resources in...
I need this computer typed so I can read it and explained ASAP please!!! In Vienna,...
I need this computer typed so I can read it and explained ASAP please!!! In Vienna, Eliud Kipchoge became the first athlete to run a marathon in less than two hours. Kipchoge wore a special type of shoe developed by Nike called Vaporfly that is different than all previous running shoes. It has a much thicker midsole and has a layer of carbon fiber to bounce back as much energy as possible. Nike engineers claim that Vaporfly is significantly faster...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT