问题描述
我试图按字母顺序排序java中的链接列表,但我的方法仅适用于整数。
我尝试了什么:
公共节点排序()
{
节点当前;
Node sortedList = null;
int count;
if(first == null)
抛出新的IllegalArgumentException(List is empty);
//创建一个遍历整个列表的循环。
for(int index = 1; index< size(); index ++)
{
//将unsorteNode变量重定向到列表中的第二个元素。
current = first.next;
//将扫描变量重新编号为索引。
count = index;
//创建一个循环请仔细阅读并交换价值。
while(count> 0&& current.value.compareTo(first.value)> 0)
{
sortedList = new Node(current.value,first);
first = first.next;
current = current.next;
count--;
}
//切换价值在正确的位置。
first.next = current;
}
//返回已排序清单。
返回sortedList;
}
I am trying to sort in alphabetical order a Linked List in java but my method only works with integers.
What I have tried:
public Node sort()
{
Node current;
Node sortedList = null;
int count;
if(first == null)
throw new IllegalArgumentException("List is empty");
// Creating a loop that will go through the whole list.
for (int index = 1; index < size(); index++)
{
// Redirects the unsorteNode variable to the second element in the list.
current = first.next;
// Redriects the scan variable to index.
count = index;
// Creating a loop that will go thorught the list and swap values.
while (count > 0 && current.value.compareTo(first.value) > 0)
{
sortedList = new Node(current.value, first);
first = first.next;
current = current.next;
count--;
}
// Switches the values in its correct position.
first.next = current;
}
// Returns the sorted list.
return sortedList;
}
这篇关于如何使我的排序方法按字母顺序排序字符串的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!