Java链表问题

时间:2014-04-11 15:27:41

标签: java

我一直在研究链表,但是遇到了一些障碍。该列表应该是包含其姓名和分数的运动员对象的表示。运动员课程是供应给我的,因此我不应该改变它。我已经完成了大部分的比赛课程,但根据标题,我现在打算从列表中打出前3名。我尝试了几种不同的方法,但没有运气。有没有人知道如何才能做到这一点?下面是源代码,竞赛和竞赛驱动程序类是我开发的类。顺便说一下,这是一个大学课程。感谢任何有建议的人!

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();

  }
}

3 个答案:

答案 0 :(得分:2)

您有两种选择:

1)浏览每个节点并确定前3个分数。 (我不推荐这个,因为它需要在你经历每个节点时跟踪前三个分数)

2)始终让您的链表按降序排序。这样,当您需要从第一个节点开始找到前三个,然后转到下两个节点。这也意味着在插入/删除时,您需要对列表进行排序。我建议查看Heap datastructure

答案 1 :(得分:1)

将此添加到您的班级比赛:

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);
}

这适用于您提供的示例。希望它也适用于所有示例。

编辑:不要忘记将此方法添加到您的主要内容中: competition.threeStrongest();

答案 2 :(得分:0)

使用以下代码:

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());
                }


            }

            }

        }

    }

和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();

      }
    }

并运行它。