我面臨着參考操作的問題: 首先這是一個代碼,其值爲x並遍歷List,刪除任何具有小於或等於X值的鏈接,但它給了我一個不規則的輸出。幫助被讚賞。單鏈表參考操作
public void rlx (int x){
Link p = head;//Initializing a pointer equal to head
for (Link c = head.next; c!=null;c=c.next) {//Initializing another Pointer with the Condition to termination
if((int)head.data<=x){//If the Value of head< = to X
head=head.next;//Skip the first and assign head to the second
}else if((int)c.data<=x){
p.next=c.next;//P.next skip c by pointing to c.next instead of c;
}
p=c; reinitialize p;
}
}
主要方法:
public static void main(String [] args){
LinkList l = new LinkList();
l.insertLast(1);
l.insertLast(2);
l.insertLast(3);
l.insertLast(4);
l.insertLast(3);
l.insertLast(2);
l.insertLast(1);
l.rlx(3);
System.out.print(l);
}
輸出:[4,2]
我們需要更多信息才能開始診斷正在發生的事情。從我看來,你*可能*也有數據插入的問題。 – Makoto
我認爲問題是數據刪除,因爲輸出Result應該是[4],因爲所有其他值都等於或小於x。我希望我可以做到這一點,而不使用其他鏈接列表。注意這個類是由我自己製作的,這個方法是內部的。@ Makoto –