替换链接列表中的项目并排序
Replacing items in a linked list and sorting
所以我在替换链接列表中的项目时遇到问题,因为我同时添加和排序。例如,我想插入数字 2、7、3,所以在执行期间,当我插入 3 时,我最终将它与 7 交换,因此列表如下所示:2、3、7。但是我现在我的代码在交换值时挂起。
public void ADD(E num) {
Node<E> temp = new Node<>(num);
if (this.head == null) {
this.head = temp;
System.out.println("Was empty");
} else {
Node<E> lead = this.head;
Node<E> tail = this.head;
while (lead != null) {
if (lead.info.compareTo(temp.info) == -1) {//If the lead.info is less than the argument then -1 is returned.
lastNode().next = temp; // adds to the end of linked list
//System.out.println("it works");
}
if (lead.info.compareTo(temp.info) == 1) { //if the greater than we swap out
Node<E> z = lead;
lead = temp;
lastNode().next = lead;
lead=lead.next;
}
lead=lead.next;
}
}
}
添加一个用于添加到前面的案例,保留一个 prev
引用(看起来您正在使用 lastNode()
,但它是如何工作的并不明显)。
要添加到末尾,请通过检查 if (lead.next == null)
以及检查最后一个值是否小于插入的值来确保您位于列表的末尾。
应该是这样的:
public void ADD(E num) {
Node<E> temp = new Node<>(num);
if (this.head == null) {
this.head = temp;
System.out.println("Was empty");
} else {
Node<E> lead = this.head;
Node<E> prev = null; //modified
//first check if it should be added at the front
if (lead.info.compareTo(temp.info) == 1) {
temp.next = this.head;
this.head = temp;
System.out.println("added to front of list");
return;
}
while (lead != null) {
//if (lead.info.compareTo(temp.info) == -1) {//If the lead.info is less than the argument then -1 is returned.
if (lead.next == null && lead.info.compareTo(temp.info) == -1){ //if at the end of the list and lead.info is less than the argument, add to the end
//lastNode().next = temp; // adds to the end of linked list
lead.next = temp;
System.out.println("added to end of list");
return; //return if node was added
}
if (lead.info.compareTo(temp.info) == 1) { //if the greater than we swap out
//Node<E> z = lead;
prev.next = temp;
temp.next = lead;
//lead = temp;
//lastNode().next = lead; //not needed if you use prev
//lead=lead.next;
System.out.println("added inside of list");
return; //return if node was added
}
prev = lead; //added this, keep prev one behind lead
lead=lead.next;
}
}
}
所以我在替换链接列表中的项目时遇到问题,因为我同时添加和排序。例如,我想插入数字 2、7、3,所以在执行期间,当我插入 3 时,我最终将它与 7 交换,因此列表如下所示:2、3、7。但是我现在我的代码在交换值时挂起。
public void ADD(E num) {
Node<E> temp = new Node<>(num);
if (this.head == null) {
this.head = temp;
System.out.println("Was empty");
} else {
Node<E> lead = this.head;
Node<E> tail = this.head;
while (lead != null) {
if (lead.info.compareTo(temp.info) == -1) {//If the lead.info is less than the argument then -1 is returned.
lastNode().next = temp; // adds to the end of linked list
//System.out.println("it works");
}
if (lead.info.compareTo(temp.info) == 1) { //if the greater than we swap out
Node<E> z = lead;
lead = temp;
lastNode().next = lead;
lead=lead.next;
}
lead=lead.next;
}
}
}
添加一个用于添加到前面的案例,保留一个 prev
引用(看起来您正在使用 lastNode()
,但它是如何工作的并不明显)。
要添加到末尾,请通过检查 if (lead.next == null)
以及检查最后一个值是否小于插入的值来确保您位于列表的末尾。
应该是这样的:
public void ADD(E num) {
Node<E> temp = new Node<>(num);
if (this.head == null) {
this.head = temp;
System.out.println("Was empty");
} else {
Node<E> lead = this.head;
Node<E> prev = null; //modified
//first check if it should be added at the front
if (lead.info.compareTo(temp.info) == 1) {
temp.next = this.head;
this.head = temp;
System.out.println("added to front of list");
return;
}
while (lead != null) {
//if (lead.info.compareTo(temp.info) == -1) {//If the lead.info is less than the argument then -1 is returned.
if (lead.next == null && lead.info.compareTo(temp.info) == -1){ //if at the end of the list and lead.info is less than the argument, add to the end
//lastNode().next = temp; // adds to the end of linked list
lead.next = temp;
System.out.println("added to end of list");
return; //return if node was added
}
if (lead.info.compareTo(temp.info) == 1) { //if the greater than we swap out
//Node<E> z = lead;
prev.next = temp;
temp.next = lead;
//lead = temp;
//lastNode().next = lead; //not needed if you use prev
//lead=lead.next;
System.out.println("added inside of list");
return; //return if node was added
}
prev = lead; //added this, keep prev one behind lead
lead=lead.next;
}
}
}