0

I've been working on a linked list, but have hit a bit of a snag. The list is supposed to be a representation of athlete objects containing their names and scores. The athlete class was supplied to me, and I am therefore not supposed to change it. I've completed most of the competition class, but according to the rubric am now supposed to print the top 3 scores from the list. I've tried a couple different approaches, but have had no luck. Does anyone have any idea as to how I could accomplish this? Below is the source code, the Competition and CompetitionDriver classes are ones of which I have developed. By the way, this is for a university course. Thanks to anyone with suggestions!

Athlete.java:

public class Athlete implements Comparable {

  private String name;
  private int score;

  public Athlete (String n, int s) {
    name = n;
    score = s;
  }

  public int getScore() {
    return score;
  }

  public String toString() {
    return name;
  }

  public int compareTo(Object other) {
    return (score - ((Athlete)other).getScore());
  }
}

Competition.java:

public class Competition {

  private Athlete current;
  private Competition next;

  public Competition() {
    current = null;
    next = null;
  }

  public Competition(Athlete currentIn, Competition nextIn) {
    current = currentIn;
    next = nextIn;
  }

  public void addToEnd(Athlete input) {
    if (current != null) {
      Competition temp = this;
      while (temp.next != null) {
        temp = temp.next;
      }
      temp.next = new Competition(input, null);
    }
    else {
      current = input;
    }
  }

  public void print() {
    Competition temp = this;
    while(temp != null) {
      System.out.println("\nAthlete name: " 
                         + temp.current.toString() 
                         + "\nAthlete score: "
                         + temp.current.getScore());
      temp = temp.next;
    }
  }     
}

CompetitionDriver.java:

public class CompetitionDriver {

  public static void main(String[] args) {

    Competition competition = new Competition();

    competition.addToEnd(new Athlete("Jane", 7));
    competition.addToEnd(new Athlete("Mark", 9));
    competition.addToEnd(new Athlete("Mary", 6));
    competition.addToEnd(new Athlete("Eve", 2));
    competition.addToEnd(new Athlete("Dan", 15));
    competition.addToEnd(new Athlete("Adam", 4));
    competition.addToEnd(new Athlete("Bob", 3));
    competition.addToEnd(new Athlete("Kathy", 8));
    competition.addToEnd(new Athlete("John", 5));
    competition.addToEnd(new Athlete("Rob", 1));

    competition.print();

    competition.printTop();

  }
}
4
  • @HansZ I think It's obvious Commented Apr 11, 2014 at 15:32
  • It might be a silly question but, you are not allowed to use LinkedList<Object>, right ? Commented Apr 11, 2014 at 15:34
  • @rpax how is it obvious? There's a function that says competition.printTop() but there's no body for it. So far, I see a linked list implementation with no max function. Commented Apr 11, 2014 at 15:38
  • Oh, I had tried going through and comparing each node, which according to the one reply is the only way (we aren't expected to reorder the list). I could probably do it, it just seemed to be excessive for the little purpose it was meant to accomplish.. Commented Apr 11, 2014 at 15:53

3 Answers 3

2

You have two options:

1) Go through each node and determine the top 3 scores. (I would not recommend this as it requires having to keep track of the top three scores as you go through each and every node)

2) Always have your linked list be sorted in descending order. This way when you need to find the top three you start from the first node, and go to the next two nodes. This also means that upon inserting/deleting you need to sort your list. I suggest looking into the Heap datastructure.

Sign up to request clarification or add additional context in comments.

Comments

1

Add this to your class Competition:

public void threeStrongest() {
    Competition temp = this;
    int best = this.current.getScore();
    int best2 = 0;
    int best3 = 0;
    int i = 0;
    while(i < 3) {

        if(temp.current.getScore() > best){
            best3 = best2;
            best2 = best;
            best = temp.current.getScore();
        }
        else if(temp.current.getScore() > best2){
            best3 = best2;
            best2 = temp.current.getScore();
        }
        else {
            best3 = temp.current.getScore();
        }

        i++;
        temp = temp.next;
    }

    while(temp != null) {

        if(temp.current.getScore() > best){
            best3 = best2;
            best2 = best;
            best = temp.current.getScore();
        }
        else if(temp.current.getScore() > best2){
            best3 = best2;
            best2 = temp.current.getScore();
        }
        else if(temp.current.getScore() > best3){
            best3 = temp.current.getScore();
        }
        temp = temp.next;
    }

    System.out.println(best + " " + best2 + " " + best3);
}

This works for the example you gave. Hopefully it works for all examples too.

EDIT: Don't forget to add this method to your main like this: competition.threeStrongest();

Comments

0

use following code for:

Compitition.java:

public class Competition {

      private Athlete current;
      private Competition next;

      public Competition() {
        current = null;
        next = null;
      }

      public Competition(Athlete currentIn, Competition nextIn) {
        current = currentIn;
        next = nextIn;
      }

      public void addToEnd(Athlete input) {
        if (current != null) {
          Competition temp = this;
          while (temp.next != null) {
            temp = temp.next;
          }
          temp.next = new Competition(input, null);
        }
        else {
          current = input;
        }
      }


     Map<Integer,String> map=new HashMap<Integer,String>(); 
     List<Integer> list=new ArrayList<Integer>();
      public void print()
      {
        Competition temp = this;
        while(temp != null) 
        {
          System.out.println("\nAthlete name: " 
                             + temp.current.toString() 
                             + "\nAthlete score: "
                             + temp.current.getScore());
          map.put(temp.current.getScore(),temp.current.toString());
          list.add(temp.current.getScore());
          temp = temp.next;
        }

      }   

        public void printTop()      
        {



            Collections.sort(list);         

            int a[]={list.get(list.size()-1),list.get(list.size()-2),list.get(list.size()-3)};

            for(int i=0;i<a.length;i++)
            {


            for (Map.Entry<Integer,String> entry : map.entrySet()) {

                if(entry.getKey().equals(a[i]))
                {
                    System.out.println(entry.getKey()+"  "+entry.getValue());
                }


            }

            }

        }

    }

and CompetitionDriver.java

public class CompetitionDriver {

      public static void main(String[] args) {

        Competition competition = new Competition();

        competition.addToEnd(new Athlete("Jane", 7));
        competition.addToEnd(new Athlete("Mark", 9));
        competition.addToEnd(new Athlete("Mary", 6));
        competition.addToEnd(new Athlete("Eve", 2));
        competition.addToEnd(new Athlete("Dan", 15));
        competition.addToEnd(new Athlete("Adam", 4));
        competition.addToEnd(new Athlete("Bob", 3));
        competition.addToEnd(new Athlete("Kathy", 8));
        competition.addToEnd(new Athlete("John", 5));
        competition.addToEnd(new Athlete("Rob", 1));

        competition.print();

       competition.printTop();

      }
    }

and run it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.