这是我关于Stack Overflow的第一个问题。我必须实现一个存储对象的链表Node类(这是在线课的作业)。我已经实例化了对象并将它们作为参数传递给我的Node类,但是当我运行我的节点时,它返回为空(null)。对我做错的任何帮助?我是新来的!
public class Node {
Donor newDonor;
Node nextNode;
Node prevNode;
private Node head;
public Node() {
}
public Node(Donor newDonor) {
nextNode = null;
prevNode = null;
this.head = null;
}
public Donor getDonor(){
return newDonor;
}
public void setNewDonor(Donor newDonor) {
this.newDonor = newDonor;
}
public void setDonor(Donor newDonor){
this.newDonor = newDonor;
}
public Node getNextNode(){
return nextNode;
}
public void setNextNode(Node nextNode){
this.nextNode = nextNode;
}
public void setPrevNode(Node prevNode) {
this.prevNode = prevNode;
}
public Node getPrevNode() {
return prevNode;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
@Override
public String toString() {
return "Node{" + "newDonor=" + newDonor + ", nextNode=" + nextNode + ", prevNode=" + prevNode + '}';
}
public void addDonor(Donor newDonor) {
Node newNode = new Node();
if(head == null){
newNode.setDonor(newDonor);
newNode.setNextNode(head);
head = newNode;
System.out.println(toString());
}
else if(newDonor.getDonorID() < head.getDonor().getDonorID()){
newNode.setNextNode(head);
head = newNode;
}
Node current = head.getNextNode();
Node previous = head;
while (current != null && newNode.getDonor().getDonorID() >=current.getDonor().getDonorID()) {
previous = current;
current = current.getNextNode();
}
newNode.setNextNode(current);
previous.setNextNode(newNode);
//System.out.println(toString());