在Java中,按字母顺序排序LinkedList,而不使用按名字和姓氏排序的集合
我正在尝试编写代码,从文件中创建按字母顺序排列的名称链接列表 姓名必须按姓排序,然后按名排序。第二层排序是我遇到的问题 到目前为止,我已经包括了我的代码,而现在我只是试图重写在Java中,按字母顺序排序LinkedList,而不使用按名字和姓氏排序的集合,java,linked-list,singly-linked-list,Java,Linked List,Singly Linked List,我正在尝试编写代码,从文件中创建按字母顺序排列的名称链接列表 姓名必须按姓排序,然后按名排序。第二层排序是我遇到的问题 到目前为止,我已经包括了我的代码,而现在我只是试图重写sortList,以便它遍历链表并在比较后添加一个节点。我以前使用集合编写了一个sortList,它运行得非常完美,现在我尝试在没有集合的情况下运行它 我的怀疑是,这比我现在做的要容易,而且我现在离代码太近了。下面是sortList和compareTo方法。。。项目构造函数只包含firstName和lastName publ
sortList
,以便它遍历链表并在比较后添加一个节点。我以前使用集合编写了一个sortList
,它运行得非常完美,现在我尝试在没有集合的情况下运行它
我的怀疑是,这比我现在做的要容易,而且我现在离代码太近了。下面是sortList
和compareTo
方法。。。项目构造函数只包含firstName
和lastName
public int compareTo(linkedListProject arg0) {
if (this.lastName.equals(arg0.getLastName()))
return this.firstName.compareTo(arg0.getFirstName());
else
return this.lastName.compareTo(arg0.getLastName());
}
编辑(更改排序列表以更准确地利用compareTo)
公共静态无效排序列表(LinkedListA项目)
{
节点温度=新节点(a);
if(head==null)
{head=temp;}
否则{
节点cur=头部;
while(cur.next==null&&cur.data.compareTo(temp)>0)
{cur=cur.next;}
if(当前下一个数据比较温度)=0)
{
下一个节点=温度;
温度=下一个温度;
}
if(temp==null)
{
节点cur2=新节点(a);
cur2.next=头部;
头=电流2;
add(cur2.getData());
}
否则{
节点cur2=新节点(a);
cur2.next=温度;
if(nextNode==null){
nextNode.next=cur2;
add(cur2.getData());
}
}
}
/**
*将学生姓名读入对象,然后根据姓氏和姓氏对列表进行添加和排序
*@param inputFileName文件名
*@抛出java.io.IOException
*/
公共无效读取(字符串inputFileName)引发java.io.IOException
{
Scanner infle=新扫描仪(新文件读取器(inputFileName));
while(infle.hasNext())
{
String firstName=infle.next();
字符串lastName=infle.next();
linkedListProject intoList=新建linkedListProject(firstName,lastName);
linkedListProject.addToList(intoList);
}
infle.close();
}
/**
*司机
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException
{
linkedListProject测试=新建linkedListProject();
test.read(“179ClassList.txt”);
test.printList();
}
公共整数比较(linkedListProject arg0)
{
if(this.lastName.equals(arg0.getLastName()))
返回这个.firstName.compareTo(arg0.getFirstName());
其他的
返回此.lastName.compareTo(arg0.getLastName());
}
}
@smcg这其实并不重要,因为家庭作业标签已被弃用。。。这是对已经提交的家庭作业的扩展。基本上,我上交的是用过的集合,但现在我想不用集合。如果可以的话,我会用家庭作业标签的。我可能知道该怎么做,使用compareTo确定字母,然后使用previous Node.next添加。@JesseWebb我知道它已被弃用,但它可能会影响某人的回答,因为“无集合”限制听起来是任意的。sortList
根本不排序(它可能是排序的子例程,但它本身不排序). 它不应该被称为insert
,insertSorted
或类似的名称吗?是的,我在过去的项目中编辑它,我没有更改它的方法名称。
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; }
}
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());
}
}