问题描述
我正在尝试编写一个代码,在不重复的情况下在双向链表中插入一个元素,但它不起作用。
任何帮助将不胜感激。 这是我的代码:
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 ;
}
解决方法
您应该添加第二种方法:
passw = input("Enter password: ")
passw2 = input("Enter password again: ")
if passw == passw2:
print("Thank you.")
elif passw.lower() == passw2.lower():
print("They must be in the same case.")
else:
print("Incorrect.")
然后将其添加到插入方法中:
public boolean isInList(int value) {
Element cur = this.head;
if(this.head == null)
return false;
while(cur != null) {
if(cur.data == value)
return true;
cur = cur.next;
}
return false;
}