总结: 注意给出待删除结点i的值不能超过索引(0~n-1),不能低于索引0.这里只需要区分是否删除头结点 和 非头结点.
node
* delAt(node
* h
, int i
)
{
if(i
<0) return h
;
node
* p
=NULL,*q
= h
;
for(int j
=0; j
<i
; j
++){
if(q
->next
== NULL) return h
;
p
= q
;
q
= q
->next
;
}
if(p
){
p
->next
= q
->next
;
delete q
;
return h
;
}
else{
h
= q
->next
;
delete q
;
return h
;
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-41897.html