*Examples:*Iflis{67,12,13,12}thenafterl.remove(12),lis{67,13}*Iflis{1,2,3}thenafterl.remove(2),lis{1,3}*Iflis{1,2,3}thenafterl.remove(99),lis{1,2,3}*/publicvoidremove(int...
* Examples:
*If l is {67,12,13,12} then after l.remove(12), l is {67,13}
*If l is {1,2,3} then after l.remove(2), l is {1,3}
*If l is {1,2,3} then after l.remove(99), l is {1,2,3}
*/
public void remove (int value)
{
if(head==null){
return;
}
while (head.getValue() == value){
head = head.getNext();
if(head == null){
tail = null;
count--;
return;
}
else{
head.setPrev(null);
count--;
}
}
IntegerNode p = head;
if(head.getNext()!= null){
IntegerNode c = head.getNext();
while(c != null){
if(c.getValue() == value){
p.setNext(c.getNext());
if(tail == c){
tail = p;
}
else{
c.getNext().setPrev(p);
}
count--;
}
p = c;
c = c.getNext();
}
}
}
我解释一下,这是一个双向链表,这个方法就是remove掉指定的元素,第一个不懂的是:
if(head==null){
return;
}
这里我为什么一定要用return?我用break就有问题,这个方法是void的,为什么可以用returned?
第二个不懂的是:
while (head.getValue() == value){
head = head.getNext();
if(head == null){
tail = null;
count--;
return;
}
这个地方的return又是什么意思,我用break的话,就实现不了指定的remove功能,我实在弄不懂break和return在这里的区别!,求高手解答!
展开