[英]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.