package com.atguigu.linkedlist;
public class DoubleLinkedList {
public static void main(String[] args) {
HeroNode2 heroNode1=new HeroNode2(0,"2","2");
HeroNode2 heroNode2=new HeroNode2(3,"3","3");
HeroNode2 heroNode3=new HeroNode2(2,"4","4");
HeroNode2 heroNode4=new HeroNode2(1,"5","5");
DouvleLiskedList douvleLiskedList = new DouvleLiskedList();
douvleLiskedList.add(heroNode1);
douvleLiskedList.add(heroNode2);
douvleLiskedList.add(heroNode3);
douvleLiskedList.add(heroNode4);
douvleLiskedList.list();
HeroNode2 heroNode5=new HeroNode2(2,"433","422");
douvleLiskedList.update(heroNode5);
System.out.println("修改后:");
douvleLiskedList.list();
douvleLiskedList.delete(heroNode5);
System.out.println("删除后:");
douvleLiskedList.list();
}
}
class DouvleLiskedList{
private HeroNode2 head=new HeroNode2(0,"","");
public HeroNode2 getHead() {
return head;
}
public void list(){
if(head.next==null){
System.out.println("链表为空");
}
HeroNode2 temp=head.next;
while (true){
System.out.println(temp);
if(temp.next==null){
break;
}
temp=temp.next;
}
}
public void add(HeroNode2 heroNode){
HeroNode2 temp=head;
while(true){
if(temp.next==null){
break;
}
temp=temp.next;
}
temp.next=heroNode;
heroNode.pre=temp;
}
public void update(HeroNode2 heroNode){
if(head.next==null){
System.out.println("链表为空");
return;
}
HeroNode2 temp=head;
boolean flag=false;
while (true){
if(temp.no==heroNode.no){
flag=true;
break;
}
if(temp.next==null){
break;
}
temp=temp.next;
}
if(flag){
temp.name= heroNode.name;
temp.nickName= head.nickName;
}else{
System.out.println("没有找到该英雄");
}
}
public void delete(HeroNode2 heroNode){
if(head.next==null){
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 temp=head.next;
boolean flag=false;
while (true){
if(temp==null){
break;
}
if(temp.no==heroNode.no){
flag=true;
break;
}
temp=temp.next;
}
if(flag){
temp.pre.next=temp.next;
if(temp.next!=null){
temp.next.pre=temp.pre;
}
}else{
System.out.println("要删除的节点不存在");
}
}
}
class HeroNode2{
public int no;
public String name;
public String nickName;
public HeroNode2 next;
public HeroNode2 pre;
public HeroNode2(int hNo,String hName,String hNickName){
this.no=hNo;
this.name=hName;
this.nickName=hNickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}