按字母顺序对LinkedList进行排序,而不使用Java中的名字和姓氏的集合

时间:2013-03-28 19:09:14

标签: java linked-list singly-linked-list

我正在尝试编写代码,该代码按字母顺序从文件创建名称的链接列表。

名称必须按姓氏排序,然后是名字。第二层排序是我遇到麻烦的地方。

到目前为止,我已经包含了我的代码,现在我只是想重写sortList,以便它通过链表并在比较后添加一个节点。我之前使用集合编写了一个sortList,它运行得很好,现在我正在尝试没有集合。

我怀疑这比我做的更容易,而且我现在距离代码太近了。以下是sortListcompareTo方法......项目构造函数只包含firstNamelastName

public int compareTo(linkedListProject arg0) {
    if (this.lastName.equals(arg0.getLastName()))
        return this.firstName.compareTo(arg0.getFirstName());
    else
        return this.lastName.compareTo(arg0.getLastName());
}

编辑(更改sortList以更准确地利用compareTo)

public static void sortList(linkedListProject a)
{
    Node temp = new Node(a);
    if (head == null)
            {head = temp;}
    else {
        Node cur = head;
        while (cur.next == null && cur.data.compareTo(temp) > 0)
        { cur = cur.next;}
    if (cur.next.data.compareTo(temp) <= 0)
        temp = cur; }
}

再次编辑...基本上我意识到我必须检查节点并插入节点,所以这是我的完整代码。 sortList已重命名为addToList。我仍然得到nullpointer异常

import java.util.*;
import java.io.*;
/**
 * @author Michael Dangelo
 * linkedListProject class, reads from file a provided classRoster then sorts them alphebetically by last name into the linkedList
 *
 */
public class linkedListProject {
private String firstName;
private String lastName;
private static LinkedList<linkedListProject> classRoster  = new LinkedList<linkedListProject>();

private static class Node
{
    linkedListProject data;
    private Node next = null;

    private Node(linkedListProject data, Node next)
    {
        this.data = data;
        this.next = next;
    }

    private Node(linkedListProject data)
    {
        this.data = data;
    }

    public linkedListProject getData()
    {
        return data;

    }

}

private static Node head;

/**
 * Default Constructor
 */
public linkedListProject()
{
    head = null;
}

/**
 * Constructor
 * @param firstName First name of student
 * @param lastName  Last Name of Student
 */
public linkedListProject(String firstName, String lastName)
{
    this.firstName = firstName;
    this.lastName = lastName;
}
public void addToStart(linkedListProject data)
{
    head = new Node(data, head);
}
/**
 * Getter for firstName
 * @return firstName
 */
public String getFirstName() 
{
    return firstName;
}

/**
 * Setter for firstName
 * @param firstName
 */
public void setFirstName(String firstName) 
{
    this.firstName = firstName;
}
/**
 * Getter for lastName
 * @return lastName
 */
public String getLastName() 
{
    return lastName;
}

/**
 * Setter for Last Name
 * @param lastName
 */
public void setLastName(String lastName) 
{
    this.lastName = lastName;
}

/**
 * toString method used for testing
 */

@Override
public String toString() 
{
    return "linkedListProject [firstName=" + firstName + ", lastName="
            + lastName + "]";
}



/**
 * compareTo method used in sorting
 */


/**
 * Prints out list to console
 */
public void printList() 
{
    int count = 0;
    System.out.println("First Name:     " + "Last Name: ");
    while (count < classRoster.size())
    {
        linkedListProject a = classRoster.get(count);
        System.out.println(a.getFirstName() + "     \t" + a.getLastName());
        count++;
    }

}
/**
 * 
 * @param a linked list ran in 
 */
public static void addToList(linkedListProject a)
{
    Node temp = head;
    Node nextNode = null;
    while (temp != null && nextNode.data.compareTo(a) >=0)
    {
        nextNode = temp;
        temp = temp.next;
    }
    if (temp == null)
    {
        Node cur2 = new Node(a);
        cur2.next = head;
        head = cur2;    
        classRoster.add(cur2.getData());
    }
    else {

        Node cur2 = new Node(a);
        cur2.next = temp;
        if (nextNode == null) {
            nextNode.next = cur2;
            classRoster.add(cur2.getData());
        }
    }
}
/**
 * Reads in students names into object then adds and sorts list based on last name & firstName
 * @param inputFileName The File Name
 * @throws java.io.IOException
 */
public void read(String inputFileName) throws java.io.IOException
{
Scanner infile = new Scanner(new FileReader(inputFileName));
while(infile.hasNext())
{
String firstName = infile.next();
String lastName = infile.next();

linkedListProject intoList = new linkedListProject(firstName, lastName);
linkedListProject.addToList(intoList);

}
infile.close();
}

/**
 * Driver
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException 
{
    linkedListProject test = new linkedListProject();
    test.read("179ClassList.txt");
    test.printList();
}

public int compareTo(linkedListProject arg0)
{
    if (this.lastName.equals(arg0.getLastName()))
        return this.firstName.compareTo(arg0.getFirstName());
    else
        return this.lastName.compareTo(arg0.getLastName());
}


}

0 个答案:

没有答案