java链表学习时,为什么不能进行节点删除。代码如下。
//建立链结点classLink{publiclongdate;publicLinknext;publicLink(longd){date=d;}publicvoiddi...
//建立链结点class Link{ public long date; public Link next; public Link(long d){ date=d; } public void displayLink(){ System.out.print(date+""); }}
//建立有序链表类
class TheSortedLink{ Link first; Link last; TheSortedLink(){ first=null; last=null; } boolean isEmpty(){ return first==null; } /** 进行插入操作的方法 **/ void insertLink(long d){ Link newLink=new Link(d); Link previous=null; Link current=first; while(current!=null&&(d>current.date)){ previous=current; current=current.next; } if(previous==null){ first=newLink; last=newLink; } else{ newLink.next=current; previous.next=newLink; } }
//进行删除指定节点的操作 void deleteLink(long d){ Link newLink=new Link(d); Link previous=null; Link current=first; while(current!=null&&d==(current.date)){ previous=current; current=current.next; } if(previous==null){ first=null; } else{ previous.next=current.next; } }
//显示列表 public void displayLink(){ System.out.print("List (first-->last:"); Link current=first; while(current!=null){ current.displayLink(); current=current.next; } System.out.println(""); } }public class SoretedLinkApp{ public static void main(String arg[]){ TheSortedLink sortedlink=new TheSortedLink(); sortedlink.insertLink(1); sortedlink.insertLink(4); sortedlink.insertLink(5); sortedlink.insertLink(2); sortedlink.insertLink(3); sortedlink.displayLink(); sortedlink.deleteLink(4); sortedlink.displayLink(); }}
是不是操作方法写错了? 展开
//建立有序链表类
class TheSortedLink{ Link first; Link last; TheSortedLink(){ first=null; last=null; } boolean isEmpty(){ return first==null; } /** 进行插入操作的方法 **/ void insertLink(long d){ Link newLink=new Link(d); Link previous=null; Link current=first; while(current!=null&&(d>current.date)){ previous=current; current=current.next; } if(previous==null){ first=newLink; last=newLink; } else{ newLink.next=current; previous.next=newLink; } }
//进行删除指定节点的操作 void deleteLink(long d){ Link newLink=new Link(d); Link previous=null; Link current=first; while(current!=null&&d==(current.date)){ previous=current; current=current.next; } if(previous==null){ first=null; } else{ previous.next=current.next; } }
//显示列表 public void displayLink(){ System.out.print("List (first-->last:"); Link current=first; while(current!=null){ current.displayLink(); current=current.next; } System.out.println(""); } }public class SoretedLinkApp{ public static void main(String arg[]){ TheSortedLink sortedlink=new TheSortedLink(); sortedlink.insertLink(1); sortedlink.insertLink(4); sortedlink.insertLink(5); sortedlink.insertLink(2); sortedlink.insertLink(3); sortedlink.displayLink(); sortedlink.deleteLink(4); sortedlink.displayLink(); }}
是不是操作方法写错了? 展开
1个回答
展开全部
//进行删除指定节点的操作
void deleteLink(long d){
Link newLink=new Link(d);
Link previous=null;
Link current=first;
while(current!=null&&d!=(current.date)){//这个地方要不等于
previous=current;
current=current.next;
}
if(previous==null){
first=null;
}
else{
previous.next=current.next;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询