我正在尝试编写一个代码,在双向链表中插入一个元素而不重复,但它不起作用。
任何帮助,将不胜感激。这是我的代码:
public void insert(int value) {
Element tmp = new Element(value);
if(this.head == null) {
this.head = this.rear = tmp;
return ;
}
if(this.head.data < value) {
tmp.next = this.head;
this.head.previous = tmp;
tmp.previous = null;
this.head = tmp;
return ;
}
if(this.rear.data > value) {
tmp.previous = this.rear;
this.rear.next = tmp;
this.rear = tmp;
return ;
}
else {
Element cur = this.head;
while(cur.next != null && cur.next.data > value)
cur = cur.next;
tmp.next = cur.next;
tmp.previous = cur;
cur.next.previous = tmp;
cur.next = tmp;
}
return ;
}