我面临着参考操作的问题: 首先这是一个代码,其值为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 –