Java 如何比较节点内的属性,以便对节点进行排序

Java 如何比较节点内的属性,以便对节点进行排序,java,sorting,linked-list,Java,Sorting,Linked List,我创建了一个自定义接口链表,每个节点都包含Patient类对象,我想比较节点内患者的属性,例如,患者严重性,以便根据严重性对链表进行排序,但我不知道如何进行。你能教我吗?多谢各位 利斯特阶级 import java.util.Comparator; import java.util.Date; /** * LList.java A class that implements the ADT list by using a chain of nodes, * with the node im

我创建了一个自定义接口链表,每个节点都包含Patient类对象,我想比较节点内患者的属性,例如,患者严重性,以便根据严重性对链表进行排序,但我不知道如何进行。你能教我吗?多谢各位

利斯特阶级

import java.util.Comparator;
import java.util.Date;

/**
 * LList.java A class that implements the ADT list by using a chain of nodes,
 * with the node implemented as an inner class.
 */
public class LList<T> implements ListInterface<T>, Comparable<Patient> {

static void SortPatient(ListInterface<Patient> patientList) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

private Node firstNode; // reference to first node
private int length;     // number of entries in list

public LList() {
    clear();
}

public LList(T[] arr) {
    clear();
    firstNode = new Node(arr[0], null);
    Node p = firstNode;
    for (int index = 1; index < arr.length; index++) {
        p.next = new Node(arr[index], null);
        p = p.next;
        //index must start with 0
        //or add(arr[index]);
    }
    length = arr.length;

}

public int getPosition(T anObject) {
    Node currentNode = firstNode;
    int position = 0;
    while (currentNode != null) {
        position++;
        if (currentNode.data.equals(anObject)) {
            return position;
        }
        currentNode = currentNode.next;
    }

    return -1;
}

public final void clear() {
    firstNode = null;
    length = 0;
}

public boolean add(T newEntry) {
    Node newNode = new Node(newEntry);
    // create the new node

    if (isEmpty()) // if empty list
    {
        firstNode = newNode;
    } else {                        // add to end of nonempty list
        Node currentNode = firstNode;                   // traverse linked list with p pointing to the current node
        while (currentNode.next != null) {      // while have not reached the last node
            currentNode = currentNode.next;
        }
        currentNode.next = newNode; // make last node reference new node
    }

    length++;
    return true;
}

public boolean add(int newPosition, T newEntry) { // OutOfMemoryError possible
    boolean isSuccessful = true;

    if ((newPosition >= 1) && (newPosition <= length + 1)) {
        Node newNode = new Node(newEntry);

        if (isEmpty() || (newPosition == 1)) {     // case 1: add to beginning of list
            newNode.next = firstNode;
            firstNode = newNode;
        } else {                                                      // case 2: list is not empty and newPosition > 1
            Node nodeBefore = firstNode;
            for (int i = 1; i < newPosition - 1; ++i) {
                nodeBefore = nodeBefore.next;       // advance nodeBefore to its next node
            }

            newNode.next = nodeBefore.next; // make new node point to current node at newPosition
            nodeBefore.next = newNode;      // make the node before point to the new node
        }

        length++;
    } else {
        isSuccessful = false;
    }

    return isSuccessful;
}

public T remove(int givenPosition) {
    T result = null;                 // return value

    if ((givenPosition >= 1) && (givenPosition <= length)) {
        if (givenPosition == 1) {      // case 1: remove first entry
            result = firstNode.data;     // save entry to be removed
            firstNode = firstNode.next;
        } else {                         // case 2: givenPosition > 1
            Node nodeBefore = firstNode;
            for (int i = 1; i < givenPosition - 1; ++i) {
                nodeBefore = nodeBefore.next;       // advance nodeBefore to its next node
            }
            result = nodeBefore.next.data;  // save entry to be removed
            nodeBefore.next = nodeBefore.next.next; // make node before point to node after the
        }                                                               // one to be deleted (to disconnect node from chain)

        length--;
    }

    return result;                   // return removed entry, or
    // null if operation fails
}

public boolean replace(int givenPosition, T newEntry) {
    boolean isSuccessful = true;

    if ((givenPosition >= 1) && (givenPosition <= length)) {
        Node currentNode = firstNode;
        for (int i = 0; i < givenPosition - 1; ++i) {
            // System.out.println("Trace| currentNode.data = " + currentNode.data + "\t, i = " + i);
            currentNode = currentNode.next;     // advance currentNode to next node
        }
        currentNode.data = newEntry;    // currentNode is pointing to the node at givenPosition
    } else {
        isSuccessful = false;
    }

    return isSuccessful;
}

public T getEntry(int givenPosition) {
    T result = null;

    if ((givenPosition >= 1) && (givenPosition <= length)) {
        Node currentNode = firstNode;
        for (int i = 0; i < givenPosition - 1; ++i) {
            currentNode = currentNode.next;     // advance currentNode to next node
        }
        result = currentNode.data;  // currentNode is pointing to the node at givenPosition
    }

    return result;
}

public boolean contains(T anEntry) {
    boolean found = false;
    Node currentNode = firstNode;

    while (!found && (currentNode != null)) {
        if (anEntry.equals(currentNode.data)) {
            found = true;
        } else {
            currentNode = currentNode.next;
        }
    }

    return found;
}

public int getLength() {
    return length;
}

public boolean isEmpty() {
    boolean result;

    if (length == 0) {
        result = true;
    } else {
        result = false;
    }

    return result;
}

public boolean isFull() {
    return false;
}

public String toString() {
    String outputStr = "";
    Node currentNode = firstNode;
    while (currentNode != null) {
        outputStr += currentNode.data;
        currentNode = currentNode.next;
    }
    return outputStr;
}

private class Node {

    private T data;
    private Node next;

    private Node(T data) {
        this.data = data;
        this.next = null;
    }

    private Node(T data, Node next) {
        this.data = data;
        this.next = next;
    }
} // end Node


}
列表接口类

public interface ListInterface<T> {

public boolean add(T newEntry);

public boolean add(int newPosition, T newEntry);

public T remove(int givenPosition);

public void clear();

public boolean replace(int givenPosition, T newEntry);

public T getEntry(int givenPosition);

public boolean contains(T anEntry);

public int getLength();

public boolean isEmpty();

public boolean isFull();

public int getPosition(T anObject);
}
病人类别

import java.util.Date;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static javafx.scene.input.KeyCode.T;

public class Patient implements Comparable<Patient> {

String patientId;
String patientName;
String patientGender;
String patientIcNumber;
String patientContactNumber;
Date date;
int seriousness;
static int count = 0;

public Patient() {
}

public Patient(String patientId, String patientName, String patientGender, String patientIcNumber, String patientContactNumber, Date date, int seriousness) {
    this.patientId = patientId;
    this.patientName = patientName;
    this.patientGender = patientGender;
    this.patientIcNumber = patientIcNumber;
    this.patientContactNumber = patientContactNumber;
    this.date = date;
    this.seriousness = seriousness;
    count++;

}

public String getPatientId() {
    return patientId;
}

public void setPatientId(String patientId) {
    this.patientId = patientId;
}

public String getPatientName() {
    return patientName;
}

public void setPatientName(String patientName) {
    this.patientName = patientName;
}

public String getPatientGender() {
    return patientGender;
}

public void setPatientGender(String patientGender) {
    this.patientGender = patientGender;
}

public String getPatientIcNumber() {
    return patientIcNumber;
}

public void setPatientIcNumber(String patientIcNumber) {
    this.patientIcNumber = patientIcNumber;
}

public String getPatientContactNumber() {
    return patientContactNumber;
}

public void setPatientContactNumber(String patientContactNumber) {
    this.patientContactNumber = patientContactNumber;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public int getSeriousness() {
    return seriousness;
}

public void setSeriousness(int seriousness) {
    this.seriousness = seriousness;
}

public int getCount() {
    return count;
}

@Override
public String toString() {
    return patientId + "  " + patientName + "  " + patientGender + "  " + patientIcNumber + "  " + patientContactNumber + "  " + date + "  " + seriousness + "\n";
}

@Override
public int compareTo(Patient t) {
    int patientSeriouness = t.seriousness;
    Date arrival = t.date;
    if (this.seriousness == patientSeriouness) {
        return this.date.compareTo(arrival);
    } else {
        return Integer.compare(this.seriousness, patientSeriouness);
    }
}

}

如果您想对列表进行排序,您可以执行以下操作:我尝试了修改冒泡排序的版本,但尚未测试,因为这只是一种方法:

static void SortPatient(ListInterface<Patient> patientList) {
    if (patientList == NULL)
    return;

   boolean swapped;
   Patient patent;

    do
    {
      swapped = false;
      patient = head;
      while (patientList.next != null)
      {
          if (patient.compareTo(patientList.next) < 1)
          {
              swap(patientList.current, patientList.next);
              swapped = true;
          }
          patient = patientList.next;
      }
    } while (swapped);
}

在哪里添加此项,我如何知道它是根据患者的严重程度进行排序?谢谢,我已经为您定义了一个方法,所以请调用此方法。请注意,我使用了patient.compareTopatientList.next,所以它使用了patient的compare to方法。所以这取决于您在compareTo方法中定义的内容。对不起。你能帮我看一下吗,代码中有错误你好。我对你的代码有点问题,你能看一下吗?那是什么?你认为在新问题中提出它会有帮助吗?所以,将它粘贴到一个新问题中,并将问题与您看到的问题一起粘贴到您尝试了什么?