新程序员在这里,我试图理解并分解下面的代码以获取删除方法,排序链表。我在下面添加了我的理解和不理解的评论。有人可以阐明不清楚的事情吗?
提前致谢。
/* 1 */ public void remove(E e) throws NotFoundException{
/* 2 */ Node<E> p; //declares node p
/* 3 */ // chunk below determines where to start traversing based on element value. should traverse from head if new element < pos value
/* 4 */ if(pos == head || pos.compareTo(e) >= 0 ){ //I do not understand 2nd equality..why?
/* 5 */ p = head; //traverse list from head
/* 6 */ }else{
/* 7 */ //traverse list from pos
/* 8 */ p = pos;
/* 9 */ }
/* 10 */ for( ;p.next!=null && p.next.compareTo(e)<0; p = p.next); //nothing to initialize?
/* 11 */ //e not found in the list
/* 12 */ if(p.next == null || p.next.compareTo(e) > 0){
/* 13 */ throw new NotFoundException();
/* 14 */ }
/* 15 */ if(p.next == pos){
/* 16 */ //if node to be deleted is pos, update pos to head
/* 17 */ pos = head;
/* 18 */ }
/* 19 */ p.next = p.next.next; //delete node
/* 20 */ }