我正在尝试实现一个链表,该链表具有一个插入函数,一旦插入节点,该函数就会返回 true。在这种情况下,概念上返回 false 的正确方法是什么?
public boolean insert(int d) {
if (head == null) {
head = new Node(d);
return true;
}
if (head.data > d) {
Node holder = head;
Node newNode = new Node(d);
head = newNode;
head.next = holder;
holder.prev = newNode;
return true;
}
Node tmpNode = head;
while (tmpNode.next != null && tmpNode.next.data < d) {
tmpNode = tmpNode.next;
}
Node prevTmp = tmpNode;
Node insertedNode = new Node(d);
if (tmpNode.next != null) {
Node nextTmp = tmpNode.next;
insertedNode.next = nextTmp;
nextTmp.prev = insertedNode;
}
prevTmp.next = insertedNode;
insertedNode.prev = prevTmp;
return true;
}