简单链表的删除与修改
class LinkedList3{
private HeroNode3 head =new HeroNode3(0," "," ");
public void add(HeroNode3 heroNode3){
HeroNode3 temp = head;
while(true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next =heroNode3;
}
public void addByOrder(HeroNode3 heroNode3){
HeroNode3 temp =head;
boolean flag =false;
while (true){
if (temp.next == null){
break;
}
if(temp.next.no > heroNode3.no){
break;
}else if (temp.next.no ==heroNode3.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
System.out.printf("编号%d已经存在\n", heroNode3.no);
}else{
heroNode3.next = temp.next;
temp.next = heroNode3;
}
}
public void update (HeroNode3 newheroNode3){
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode3 temp = head.next;
boolean flag = false;
while (true){
if(temp == null){
break;
}
if(temp.no == newheroNode3.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.name = newheroNode3.name;
temp.nickname =newheroNode3.nickname;
}else{
System.out.printf("没有找到 编号为/5d的节点 , 不能修改", newheroNode3.no);
}
}
public void delete(int no){
HeroNode3 temp = head;
boolean flag = false;
while(true){
if(temp.next == null){
break;
}
if(temp.next.no ==no){
flag = true ;
break;
}
temp = temp.next;
}
if(flag){
temp.next = temp.next.next;
}else{
System.out.printf("要删除的节点%d不存在",no);
}
}
public void list(){
if(head.next == null){
System.out.println("链表为空");
return ;
}
HeroNode3 temp = head.next;
while (true){
if(temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
}
class HeroNode3{
public int no;
public String name ;
public String nickname ;
public HeroNode3 next ;
public HeroNode3(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}