韩顺平老师之双链表在表末尾添加节点实现
public void add(heroNode node){
heroNode temp = headNode;
while(true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp = node;
}
public void del(int N){
heroNode temp = headNode.next;
boolean flag = true;
if(temp == null){
System.out.println("链表为空,无法删除");
return ;
}
while(true){
if(temp == null){
flag = false;
break;
}
if(temp.no == N){
break;
}
temp = temp.next;
}
if(flag){
temp.pre.next = temp.next;
if(temp.next != null){
temp.next.pre = temp.pre;
}
}else{
System.out.println("没有找到该节点");
}
}
public void list(){
heroNode temp = headNode.next;
if(temp == null){
System.out.println("链表为空,无法遍历");
}
while(true){
if(temp.next == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
如有错误请指正。
|