Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 60  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Selection sort Doubly linked list java
  2. public class LinkedList {
  3.         public Node first;
  4.         public Node last;
  5.  
  6.         public LinkedList() {
  7.             first = null;
  8.             last = null;
  9.         }
  10.  
  11.         public boolean isEmpty() {
  12.             return first == null;
  13.         }
  14.  
  15.         public void addFirst(Student student) {
  16.             Node newNode = new Node(student);
  17.             if (isEmpty())
  18.                 last = newNode;
  19.             else
  20.                 first.previous = newNode;
  21.             newNode.next = first;
  22.             first = newNode;
  23.         }
  24.  
  25.         public void addLast(Student student) {
  26.             Node newNode = new Node(student);
  27.             if (isEmpty())
  28.                 first = newNode;
  29.             else
  30.                 last.next = newNode;
  31.             newNode.previous = last;
  32.             last = newNode;
  33.         }
  34.  
  35.         public void display() {
  36.             Node current = last;
  37.             while (current != null) {
  38.                 System.out.print(current.student.name + "b");
  39.                 System.out.print(current.student.surname + "b");
  40.                 System.out.println(current.student.educationType);
  41.                 current = current.previous;
  42.             }
  43.         }
  44.        
  45. public void Sort() {
  46.             LinkedList list = new LinkedList();
  47.             Node toStart = last;
  48.             while (toStart!=null){
  49.                 list.addLast(findSmallest(toStart).student);
  50.                 toStart = toStart.previous;
  51.             }
  52.  
  53.  
  54.         }
  55.        
  56. public Node findSmallest(Node toStartFrom) {
  57.             Node current = toStartFrom;
  58.             Node smallest = toStartFrom; //if i put here `last` it will work correctly
  59.             while(current != null) {
  60.                 if (smallest.student.name.compareToIgnoreCase(current.student.name) > 0) smallest = current;
  61.                 current = current.previous;
  62.             }
  63.             return smallest;
  64.         }
  65.  
  66.     }
  67.  
  68.   public class Node {
  69.         public Student student;
  70.  
  71.         public Node next;
  72.         public Node previous;
  73.  
  74.         public Node(Student student) {
  75.             this.student = student;
  76.         }
  77.     }
  78.  
  79.  
  80.     public class Student {
  81.         public String name;
  82.         public String surname;
  83.         public String educationType;
  84.  
  85.         static public Student createStudent() {
  86.          ....
  87.             return student;
  88.         }
  89.     }