[英]Linked List Delete Method
以下是我的鏈接列表的類定義。 我運行一個測試程序,創建一個新的LinkedList並插入數字“3,2,1”,然后打印列表。 這很好用。 但是,當我嘗試刪除“3”或“2”時,刪除方法永遠不會完成。 當我嘗試刪除“1”時,它只打印出完整列表,就像沒有刪除任何內容一樣。
public class LinkedListTest implements LinkedList {
private Node head;
public LinkedListTest(){
head = new Node();
}
public void insert(Object x){
if (lookup(x).equals(false)){
if (head.data == null)
head.data = x;
else{
//InsertLast
Node temp = head;
while (temp.next != null){
temp = temp.next;
}
Node NewNode = new Node();
NewNode.data = x;
NewNode.next = null;
temp.next = NewNode;
}
}
//Runtime of insert method will be n, where n is the number of nodes
}
public void delete(Object x){
if (lookup(x).equals(true)){
if (head.data == x)
head = head.next;
else{
Node temp = head;
while (temp.next != null){
if ((temp.next).data == x)
temp.next = (temp.next).next;
else
temp = temp.next;
}
}
}
}
public Object lookup(Object x){
Node temp = head;
Boolean search = false;
if (head.data == x)
search = true;
while (temp.next != null){
if (temp.data == x){
search = true;
}
else{
temp = temp.next;
}
}
return search;
}
public boolean isEmpty(){
if (head.next == null && head.data == null)
return true;
else
return false;
}
public void printList(){
Node temp = head;
System.out.print(temp.data + " ");
while (temp.next != null){
temp = temp.next;
System.out.print(temp.data + " ");
}
}
}
編輯:這是節點類:
public class Node {
public Object data;
public Node next;
public Node(){
this.data = null;
this.next = null;
}
}
這里有一些問題。
第一個大問題是在lookup()
和delete()
方法中,當成功條件發生時,不會中斷循環。 這就是你的程序掛起的原因; 它處於無限循環中。
同樣值得注意的是,這一點是一種非常糟糕的做法,不使用所有if / else語句的花括號。 沒有理由不這樣做,並且當你不這樣做時它可以很容易地引入bug。
在lookup()
你應該有:
if (head.data == x) {
search = true;
} else {
while (temp.next != null){
if (temp.data == x){
search = true;
break;
} else {
temp = temp.next;
}
}
}
並在delete()
:
if (head.data == x) {
head = head.next;
} else {
Node temp = head;
while (temp.next != null) {
if (temp.next.data.equals(x)) {
temp.next = temp.next.next;
break;
} else {
temp = temp.next;
}
}
}
現在這將產生你所期望的:
public static void main( String[] args )
{
LinkedListTest llt = new LinkedListTest();
llt.insert(1);
llt.insert(2);
llt.insert(3);
llt.printList();
System.out.println();
llt.delete(2);
llt.printList();
}
輸出:
1 2 3
1 3
但是,這不會暴露您的第二個更大的問題。 您在查看節點data
時使用==
比較參考值 。
由於自動裝箱小整數值的副作用,此刻“有效”; 你得到了相同的對象引用。 (由於字符串池,字符串文字也會“起作用”)。 有關這方面的更多信息,請參閱如何比較Java中的字符串和比較java中的兩個整數時自動取消裝箱發生
我們來看看這個:
public static void main( String[] args )
{
LinkedListTest llt = new LinkedListTest();
llt.insert(1000);
llt.insert(2000);
llt.insert(2000);
llt.insert(3000);
llt.printList();
System.out.println();
llt.delete(2000);
llt.printList();
}
輸出:
1000 2000 2000 3000
1000 2000 2000 3000
lookup()
停止工作,允許插入副本。 delete()
停止了工作。
這是因為int
值超過127自動框到唯一的Integer
對象而不是緩存的對象(請參閱上面鏈接的SO問題以獲得完整的解釋)。
您正在使用的任何地方==
比較data
所持有的值需要更改為使用.equals()
。
if (temp.data.equals(x)) {
解決了這些技術問題后,您的程序將正常運行。 還有其他事情你應該考慮。 跳出來的兩個是:
lookup
應該返回一個boolean
。 delete()
調用lookup()
delete()
lookup
本身作為一種單獨的方法是一種相當低效的方法; 插入遍歷整個列表兩次。 首先為什么查找返回一個對象? 將其更改為布爾值。 lookup()
中的“while”循環也不會前進。 你需要刪除“其他”。 你的刪除功能似乎很好。
我覺得你有些不對勁。
首先,當您使用LinkedList時,您嘗試實現的所有功能都已存在。
此外,你的風格相對較差。 例如,為什么要使用WrapperClass布爾值進行查找?
將像contains這樣的功能與get in one方法結合起來並不是一個好主意。 將它拆分為兩個方法,let contains只返回一個布爾值並測試列表中是否存在元素。 讓我們搜索並返回一個元素。
除此之外,您嘗試通過相等比較對象。 如果你沒有覆蓋equals,你就不能刪除任何東西,因為equals需要引用相等而不是大多數時候給出的。
我強烈建議你購買一本java書或其他東西來提高你的整體知識。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.