這是具有生成雙端串列、洗掉和顯示其元素的方法的Main類。main
public class Main {
Link first, last;
public static void main(String args[]) {
Main ob = new Main();
Link arr[] = {
new Link(1), new Link(2), new Link(3)
};
int len = 3;
for(int i=0;i<len;i )
ob.insertFirst(arr[i]);
System.out.print("Data in the list: ");
while(ob.first!=null)
System.out.print(ob.removeAndReturn() ", ");
for(int i=0;i<len;i )
ob.insertLast(arr[i]);
System.out.print("\nData in the list: ");
while(ob.first!=null)
System.out.print(ob.removeAndReturn() ", ");
}
void insertFirst(Link arg) {
if(isEmpty())
last = arg;
arg.next = first;
first = arg;
}
// This removeAndReturn() method returns the Object data the link is holding and removes that Link from the list
Object removeAndReturn() {
Object ret = null;
try {
ret = first.data;
if(first.next==null)
last = null;
first = first.next;
}catch(NullPointerException NPe) {
System.out.println("You are referring to a null.\nLinked List is empty.");
}
return ret;
}
void insertLast(Link arg) {
if(isEmpty())
first = arg;
else
last.next = arg;
last = arg;
}
boolean isEmpty() {
return first==null;
}
}
class Link {
Object data;
Link next;
Link(Object data) {
this.data = data;
}
}
執行時,它給出以下輸出:
Data in the list: 3, 2, 1,
Data in the list: 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, ... {truncated}
這里最后兩個元素在輸出中重復。我嘗試在呼叫之前取消這兩個Link變數first,但它給出了相同的輸出。lastob.insertLast(arr[i])
更新:
- 私有關鍵字從
Main類中方法的完整方法簽名中洗掉,而不是main(String args[])method 和rmF()method 更改為removeAndReturn().
uj5u.com熱心網友回復:
您的代碼中的主要問題在于您使用完全相同的節點(在 中的節點arr)用頭尾插入填充您的串列。
事實上,一旦你執行了第一次頭部插入,這些節點就會像這樣相互鏈接:
(3) => (2) => (1) => null
因此,當您執行第二次插入時,節點 1 指向節點 2,節點 2 指向節點 3,理論上節點 3 應該指向 null,因為它應該是最后一個元素。但是,節點 3 的下一個欄位仍然指向上次插入的節點 2。這將創建一個回圈,其中節點 2 和節點 3 保持相互指向;從而產生您正在經歷的無限回圈。
(1) => (2) => <= (3)
要解決您的問題,您可以next在重新使用節點之前重置節點的欄位(糟糕的解決方案),或者使用您需要構建的實際資料而不是節點。實際上,您班級的用戶不應該為您的實作細節而煩惱,而應該只關心要存盤/表示的資訊(在您的案例int編號中)。
這是您的問題的可能解決方案:
public class Main {
Link first, last;
public static void main(String args[]) {
Main ob = new Main();
//Array of int not of links
int[] arr = {1, 2, 3};
int len = 3;
for (int i = 0; i < len; i )
ob.insertFirst(arr[i]);
System.out.print("Data in the list: ");
while (ob.first != null)
System.out.print(ob.removeAndReturn() ", ");
for (int i = 0; i < len; i )
ob.insertLast(arr[i]);
System.out.print("\nData in the list: ");
while (ob.first != null)
System.out.print(ob.removeAndReturn() ", ");
}
//------ CORRECTION ------
//The method should receive the info the user needs to store.
//It will then be up to you to represent it as a Link or whatever
//internal structure you're going to use tomorrow. Don't bind the
//user to your internal implementation.
//------------------------
void insertFirst(int arg) {
//Generating a new node (or link) based on the given info
Link l = new Link(arg);
if (isEmpty())
last = l;
l.next = first;
first = l;
}
// This removeAndReturn() method returns the Object data the link is holding and removes that Link from the list
Object removeAndReturn() {
Object ret = null;
try {
ret = first.data;
if (first.next == null)
last = null;
first = first.next;
} catch (NullPointerException NPe) {
System.out.println("You are referring to a null.\nLinked List is empty.");
}
return ret;
}
//-------- CORRECTION --------
//same explanation given above
//----------------------------
void insertLast(int arg) {
//Generating a new node (or link) based on the given info
Link l = new Link(arg);
if (isEmpty())
first = l;
else
last.next = l;
last = l;
}
boolean isEmpty() {
return first == null;
}
}
最后,不要捕獲RuntimeException. 這些是未經檢查的例外,未經檢查。您應該調查它們的來源,而不是簡單地抓住它們。你寫的是不好的做法。
https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
正如@JayC667 已經說過的,您可以改進類、方法和變數的一些設計和命名。有一些約定,尤其是在談論資料結構時。例如:
你的類被呼叫了,
Main但它描述了一個List,像這樣的名字MyList會更好。您的實用程式類 ,可以作為靜態嵌套類
Link放置在其中,并且可能已命名(它更適合)。MyListNode你的一些方法的名字有點太神秘了。不言自明的名稱將更好地幫助您班級的用戶。
避免從外部(
list.first != null)訪問另一個物件的內部狀態。方法應該是您詢問物件狀態的方式。使用泛型型別可能是比
Object泛型提供的更好的實作:在編譯時進行嚴格檢查,避免強制型別安全性更高,能夠為不同的資料型別重用相同的代碼。
https://docs.oracle.com/javase/tutorial/java/generics/why.html
以下是包含上述建議的實作的鏈接:
https://www.jdoodle.com/iembed/v0/s7C
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489535.html
