Note that there are some explanatory texts on larger screens.

plurals
  1. POSelection sort Doubly linked list java
    primarykey
    data
    text
    <p>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. </p> <pre><code> 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; } } </code></pre> <p>Because of non-working <code>findSmallest</code> method <code>Sort</code> 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" </p> <pre><code> public void Sort() { LinkedList list = new LinkedList(); Node toStart = last; while (toStart!=null){ list.addLast(findSmallest(toStart).student); toStart = toStart.previous; } } </code></pre> <p>It sends biggest element added and if I manually assign'last' to'smallest' it would work.</p> <pre><code> 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) &gt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload