Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked Lists & quickSort
    primarykey
    data
    text
    <p>I was trying another approach to sort a linked list. Aside from the available methods, i decided to take each node from the linked list and place it in an array and by that i would be able to compare the data variables easily. I applied the quickSort on the array and this is what i got...however, when the result is displayed, I'm getting the memory address of the nodes and not the Students information</p> <p>This is what is appearing: (as an output)</p> <p>Sorted list is:</p> <pre><code>homework2.Node@44b471fe homework2.Node@22a7fdef homework2.Node@431067af homework2.Node@6a07348e null </code></pre> <p>This is my code.</p> <pre><code>public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); Student s = new Student(1, "John", 20, "Italy", "2011"); list.addStudent(s); Student s2 = new Student(2, "Mark", 19, "UAE", "2010"); list.addStudent(s2); Student s3 = new Student(3, "Sally", 35, "UAE", "2000"); list.addStudent(s3); System.out.println("Students in the list: "); list.print(); Node[] n = list.convertA(list); quickSort(n, 0, (n.length-1)); System.out.println("Sorted list is:"); for(int q =0;q&lt;n.length;q++){ System.out.println(n[q] + " "); } } public static int partition(Node arr[], int left, int right) { int i = left, j = right; Node tmp; Node pivot = arr[(left + right) / 2]; while (i &lt;= j) { while (arr[i].getStudent().getAge() &lt; pivot.getStudent().getAge()) { i++; } while (arr[j].getStudent().getAge() &gt; pivot.getStudent().getAge()) { j--; } if (i &lt;= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } return i; } public static void quickSort(Node arr[], int left, int right) { int index = partition(arr, left, right-1); if (left &lt; index - 1) { quickSort(arr, left, index - 1); } if (index &lt; right) { quickSort(arr, index, right); } } </code></pre> <p>the Node class is as follows:</p> <pre><code>public class Node { private Student student; public Node link; public Node() { student = null; link = null; } public Node(Student s) { student = s; } public Node(Student s, Node l) { student = s; link = l; } public Object getData() { return student.toString(); } public Student getStudent() { return student; } public void setLink(Node link) { this.link = link; } public void setStudent(Student student) { this.student = student; } @Override public String toString() { return this.student.toString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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