[英]Using a method to sort a Singly Linked List in Java
实施链接列表,最多可存储10个名称,先进先出顺序排列。 然后实现两种方法,一种方法是按姓氏的字母顺序对其进行排序。 这是我遇到麻烦的地方。 这是我尝试过的:
递归。 该方法调用两个节点,比较它们,如果需要交换,然后调用自身。 不能使用奇数个名称,并且往往是完整的错误。
收藏,但我对它的了解不足以有效地使用它。
排序算法(例如冒泡排序):我可以浏览列表,但是很难交换节点。
我的问题是:最简单的方法是什么?
public class List
{
public class Link
{
public String firstName;
public String middleName;
public String lastName;
public Link next = null;
Link(String f, String m, String l)
{
firstName = f;
middleName = m;
lastName = l;
}
}
private Link first_;
private Link last_;
List()
{
first_ = null;
last_ = null;
}
public boolean isEmpty()
{
return first_ == null;
}
public void insertFront(String f, String m, String l)
{
Link name = new Link(f, m, l);
if (first_ == null)
{
first_ = name;
last_ = name;
}
else
{
last_.next = name;
last_ = last_.next;
}
}
public String removeFront()
{
String f = first_.firstName;
String m = first_.middleName;
String l = first_.lastName;
first_ = first_.next;
return f + " " + m + " " + l;
}
public String findMiddle(String f, String l)
{
Link current = first_;
while (current != null && current.firstName.compareTo(f) != 0 && current.lastName.compareTo(l) != 0)
{
current = current.next;
}
if (current == null)
{
return "Not in list";
}
return "That person's middle name is " + current.middleName;
}
}
public class NamesOfFriends
{
public static void main(String[] args)
{
List listOfnames = new List();
Scanner in = new Scanner(System.in);
for(int i = 0; i < 3; i++)
{
if(i == 0)
{
System.out.println("Please enter the first, middle and last name?");
listOfnames.insertFront(in.next(), in.next(),in.next());
}
else
{
System.out.println("Please enter the next first, middle and last name");
listOfnames.insertFront(in.next(), in.next(),in.next());
}
}
System.out.println("To find the middle name, please enter the first and last name of the person.");
System.out.println(listOfnames.findMiddle(in.next(),in.next()));
}
}
经过一番努力之后,我想出了如何对其进行排序。 为此,我试图实现一个remove方法,该方法可以删除列表上任何位置的节点。 虽然可以编译,但是在运行程序时它什么也没做。
public Link remove(String lastName)
{
Link current_ = first_;
Link prior_ = null;
Link temp_ = null;
while (current_ != null && current_.lastName.compareTo(lastName) != 0)
{
prior_ = current_;
current_ = current_.next;
}
if (current_ != null)
{
if (current_ == last_)
{
temp_ = last_;
last_ = prior_;
}
else if (prior_ == null)
{
temp_ = first_;
first_ = first_.next;
}
}
else
{
temp_ = current_;
prior_.next = current_.next;
}
return temp_;
}
2:最简单的是收藏,但是在您的家庭作业中似乎是不允许的
3:BubbleSort很简单,但是排序算法最差,但是对于您的作业来说可能还可以
1:这与冒泡排序相同,但最好不进行递归
在BubbleSort中,您一次又一次地循环遍历您的元素,直到不再需要交换时,您才准备就绪。
收集是完成此操作的最简单方法。
Comparable
hashcode
并equals
Collection.sort()
您已经实现了链表,这很好。
您是否考虑过将MergeSort作为排序算法? 作为分而治之的算法,您最终总是只用两个元素组成一个列表。
合并部分将比较棘手,但也很容易。 基本上,您只是创建一个新列表,并通过比较两个合并集的第一个值开始使用获得的元素来填充它。
因此,例如,如果有两个要合并的集合:
[A]->[C]->[D]
[B]->[E]->[F]
合并过程将进行:
[A]
[C]->[D]
[B]->[E]->[F]
[A]->[B]
[C]->[D]
[E]->[F]
[A]->[B]->[C]
[D]
[E]->[F]
[A]->[B]->[C]->[D]
[E]->[F]
[A]->[B]->[C]->[D]->[E]
[F]
[A]->[B]->[C]->[D]->[E]->[F]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.