选择排序双向链表java
我需要使用选择排序对链接列表进行排序。 但我不能使用集合。 我在查找最小元素和创建新版本的排序列表时遇到麻烦。 谢谢。
public class LinkedList {
public Node first;
public Node last;
public LinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
return first == null;
}
public void addFirst(Student student) {
Node newNode = new Node(student);
if (isEmpty())
last = newNode;
else
first.previous = newNode;
newNode.next = first;
first = newNode;
}
public void addLast(Student student) {
Node newNode = new Node(student);
if (isEmpty())
first = newNode;
else
last.next = newNode;
newNode.previous = last;
last = newNode;
}
public void display() {
Node current = last;
while (current != null) {
System.out.print(current.student.name + "\b");
System.out.print(current.student.surname + "\b");
System.out.println(current.student.educationType);
current = current.previous;
}
}
由于 findSmallest
方法不工作,Sort
方法无法正常工作。我尝试通过创建一个新列表来实现排序,在该列表中以排序方式放置节点。而且它也不会脱离“While 循环”,
public void Sort() {
LinkedList list = new LinkedList();
Node toStart = last;
while (toStart!=null){
list.addLast(findSmallest(toStart).student);
toStart = toStart.previous;
}
}
它会发送添加的最大元素,如果我手动将“最后”分配给“最小”,它将起作用。
public Node findSmallest(Node toStartFrom) {
Node current = toStartFrom;
Node smallest = toStartFrom; //if i put here `last` it will work correctly
while(current != null) {
if (smallest.student.name.compareToIgnoreCase(current.student.name) > 0) smallest = current;
current = current.previous;
}
return smallest;
}
}
public class Node {
public Student student;
public Node next;
public Node previous;
public Node(Student student) {
this.student = student;
}
}
public class Student {
public String name;
public String surname;
public String educationType;
static public Student createStudent() {
....
return student;
}
}
I need to Sort Linked list using Selection sort.
But I can not use Collections.
I have a troubles with finding smallest elements and creating a new version of sorted list.
Thanks.
public class LinkedList {
public Node first;
public Node last;
public LinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
return first == null;
}
public void addFirst(Student student) {
Node newNode = new Node(student);
if (isEmpty())
last = newNode;
else
first.previous = newNode;
newNode.next = first;
first = newNode;
}
public void addLast(Student student) {
Node newNode = new Node(student);
if (isEmpty())
first = newNode;
else
last.next = newNode;
newNode.previous = last;
last = newNode;
}
public void display() {
Node current = last;
while (current != null) {
System.out.print(current.student.name + "\b");
System.out.print(current.student.surname + "\b");
System.out.println(current.student.educationType);
current = current.previous;
}
}
Because of non-working findSmallest
method Sort
method doesn't work correctly. I try to implelent sorting by creating a new list where I put Nodes in sorted way. And it also doesn't go out of "While loop"
public void Sort() {
LinkedList list = new LinkedList();
Node toStart = last;
while (toStart!=null){
list.addLast(findSmallest(toStart).student);
toStart = toStart.previous;
}
}
It sends biggest element added and if I manually assign'last' to'smallest' it would work.
public Node findSmallest(Node toStartFrom) {
Node current = toStartFrom;
Node smallest = toStartFrom; //if i put here `last` it will work correctly
while(current != null) {
if (smallest.student.name.compareToIgnoreCase(current.student.name) > 0) smallest = current;
current = current.previous;
}
return smallest;
}
}
public class Node {
public Student student;
public Node next;
public Node previous;
public Node(Student student) {
this.student = student;
}
}
public class Student {
public String name;
public String surname;
public String educationType;
static public Student createStudent() {
....
return student;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有双向链表可能会有所帮助,因为这样您需要维护的链接就会减少。另外,您可能在使用 findSmallest() 方法时遇到问题,因为您最初将当前节点和最小节点设置为同一节点,因此当 if(smallest.student.name.compareToIgnoreCase(current.student.name) > 0) 语句为执行时,您正在将学生的姓名与学生的同名进行比较。例如,如果设置为最小的节点的学生姓名为 John,则当前设置为同一节点,则当前的学生姓名也是 John。如果他们是同名的不同学生,则不是问题,但在您的代码中,他们是同一学生,并且当前节点和最小节点都指向同一节点。实际上,这个 if 语句总是错误的,并且您永远不会执行沿列表移动当前的代码。这也是为什么当您设置smallest=last时,该方法至少在某些时候确实有效。
It might be helpful to not have a doubly linked list because then you have less links that you need to maintain. Also you might be having trouble with the findSmallest() method because you are initially setting you current and smallest to the same node so when the if(smallest.student.name.compareToIgnoreCase(current.student.name) > 0) statement is executed you are comparing the name of the student to the same name of the student. For example if the node that smallest is set to has a student name of John well current is set to the same node so the student name of current is also John. Not an issue if they are different students with the same name but in your code they are the same student and both current and smallest point to the same node. Effectively this if statement is always going to be false and you will never execute the code to move current along the list. That is also why when you set smallest = last the method does work at least some of the time.
如上所述,尝试类似
smallest = startnode
下一个 =startnode.next
while(下一个!= null)
将下一个与最小的进行比较,并相应地分配
next = next.next
将其转换为代码应该不会太难
As the above said, try something like
smallest = startnode
next =startnode.next
while(next != null)
compare next with smallest, and assign accordingly
next = next.next
Shouldn't be too hard to turn this into code