需要帮助按字母顺序排序SingleLinkedList

时间:2016-03-23 05:53:37

标签: java comparator compareto

我的程序是读取文本文件并将名称插入单个链接列表并按字母顺序打印名称。名称类中有一个需要在singlelinkedlist类中使用的compareTo和equals方法。在singleLinkedList类中,我使用addToList方法来初始创建列表。我设法将每个名字都列入列表但我不知道如何使用compareTo方法在singlelinkedlist类中按字母顺序对列表进行排序。这是我的代码: 名称等级:

     public class Name implements Comparator<Name> {
       private String firstName;
       private String lastName;
    public Name(String firstName, String lastName) {
       super();
       this.firstName = firstName;
       this.lastName = lastName;
    }
    public String toString(){
       return firstName + " " + lastName;
    }
   public int compareTo(Name name){
      return (this.firstName).compareTo(name.firstName);
   }
    /**
    * @return the firstName
    */
    public String getFirstName() {
       return firstName;
    }
    /**
    * @param firstName the firstName to set
    */
    public void setFirstName(String firstName) {
       this.firstName = firstName;
    }
    /**
    * @return the lastName
    */
    public String getLastName() {
       return lastName;
    }
    /**
    * @param lastName the lastName to set
    */
    public void setLastName(String lastName) {
       this.lastName = lastName;
    }
    }

SingleLinkedList类:

public class SingleLinkedList {
   private static Node head;
   private int listCount;
/**
* No-arg constructor
*/
public SingleLinkedList()
{
}
/**
* adds node to start
* @param name
*/
public void addToStart(Name name)
{
   head = new Node(name, head);
   listCount++;
}
/**
* adds node to list
* @param name
*/
public void addToList(Name name)
{
if(head != null)
{
   Node curr = head;
while(curr.next != null)
{
   curr = curr.next;
}
   curr.next = new Node(name);
}
else
{
   head = new Node(name);
}
   listCount++;
}
/**
* Number of nodes in the list
* 
* @return list count
*/
public int size()
{
   return listCount;
}
public String toString()
{
   StringBuilder sb = new StringBuilder();
   Node curr = head;
if(curr != null)
{
while(curr.next != null)
{
   sb.append(curr.name.toString());
   sb.append("\n");
   curr = curr.next;
}
   sb.append(curr.name.toString());
}
   return sb.toString();
}
private class Node
{
   private Name name;
   private Node next;
public Node(Name name)
{
   this.name=name;
   next = null;
}
public Node(Name name, Node next)
{
   this(name);
   this.next = next;
}
}
}

主要驱动程序:

public class ClassListDriver {

/**
* @param args
*/
public static void main(String[] args) {
String filename="179ClassList.txt";
Scanner inputStream=null;
SingleLinkedList sll= new SingleLinkedList();
try
{
   inputStream=new Scanner(new File(filename));
}
catch(FileNotFoundException e){
   System.out.println("Error opening the file.");
   System.exit(0);
}
while(inputStream.hasNextLine()){
   String[] data=inputStream.nextLine().split(" ");
   String firstName=data[0];
   String lastName=data[1];
   Name name= new Name(firstName,lastName);
   sll.addToList(name);
}
System.out.println(sll);
System.out.println(sll.size());
}

}

1 个答案:

答案 0 :(得分:0)

您需要在Name类上实现比较器。

public class Name implements Comparator<Name>{

// Overriding the compareTo method
   public int compareTo(Name name){
      return (this.firstName).compareTo(name.firstName);
   }
}