1、节点构建
class HeroNode2{
private int no;
private String name;
private String nickName;
HeroNode2 next;
HeroNode2 pre;
public HeroNode2(int no,String name,String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickName=" + nickName + "]";
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
}
2、链表构建
class DoubleLinkList{
HeroNode2 headNode2 = new HeroNode2(0,"","");
public HeroNode2 getHeadNode2() {
return headNode2;
}
//遍历
public void list() {
if(headNode2.next == null) {
System.out.println("尚无节点!");
return;
}
HeroNode2 temp = headNode2.next;
while (true) {
if(temp == null) {
break;
}
System.out.println(temp.toString());
temp = temp.next;
}
}
//添加节点到双链表的尾部
public void add(HeroNode2 heroNode) {
//判断是否为空
if(headNode2.next == null) {
headNode2.next = heroNode;
return;
}
//遍历链表节点,找到最后一个节点,插入;
//头节点不能动,所以新生成个变量,作为遍历节点的工具
HeroNode2 temp = headNode2;
while(true) {
if(temp.next == null) {//找到了,插入此位置
temp.next = heroNode;
heroNode.pre = temp;
break;
}
//没找到,继续遍历
temp = temp.next;
}
}
//修改
public void update(HeroNode2 newHeroNode) {
//判空
if(headNode2.next == null) {
System.out.println("链表为空,不能修改!");
return;
}
//定义临时变量,作为遍历变量
HeroNode2 temp = headNode2.next;
boolean flage = false;//是否找到待修改的节点
while (true) {
if(temp == null) {//遍历到链表尾
break;
}
if(temp.getNo() == newHeroNode.getNo()) {
flage = true;
break;
}
temp = temp.next;
}
if(flage) {
temp.setName(newHeroNode.getName());
temp.setNickName(newHeroNode.getNickName());
System.out.printf("节点no为%d的节点修改成功!\n",newHeroNode.getNo());
}else {
System.out.printf("节点no为%d的节点修改失败!\n",newHeroNode.getNo());
}
}
//删除
//删除节点
public void delete(int no) {
if(headNode2.next == null) {
System.out.println("链表为空,不能删除!");
}
HeroNode2 temp = headNode2.next;
boolean flage = false;
while(true) {
if(temp == null) {
break;
}
if(temp.getNo() == no) {//找到了
flage = true;
break;
}
temp = temp.next;
}
if(flage) {
temp.pre.next = temp.next;
if(temp.next!=null) {
temp.next.pre = temp.pre;
}
System.out.printf("链表节点no为%d的节点成功被删除!\n",no);
}else {
System.out.printf("链表节点no为%d的节点未找到!\n",no);
}
}
}
3、测试
public static void main(String[] args) {
// TODO Auto-generated method stub
// 案例1、宋江、吴用、林冲
HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 heroNode2 = new HeroNode2(2, "吴用", "智多星");
HeroNode2 heroNode3 = new HeroNode2(3, "林冲", "豹子头");
HeroNode2 heroNode4 = new HeroNode2(4, "宋江", "及时雨");
HeroNode2 heroNode5 = new HeroNode2(5, "吴用", "智多星");
HeroNode2 heroNode6 = new HeroNode2(6, "林冲", "豹子头");
HeroNode2 heroNode7 = new HeroNode2(7, "宋江", "及时雨");
HeroNode2 heroNode7_ = new HeroNode2(7, "宋江", "及时雨");
HeroNode2 heroNode8 = new HeroNode2(8, "吴用", "智多星");
HeroNode2 heroNode9 = new HeroNode2(9, "林冲", "豹子头");
HeroNode2 heroNode10 = new HeroNode2(10, "宋江", "及时雨");
HeroNode2 heroNode11 = new HeroNode2(11, "吴用", "智多星");
HeroNode2 heroNode12 = new HeroNode2(12, "林冲", "豹子头");
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.add(heroNode1);
doubleLinkList.add(heroNode2);
doubleLinkList.add(heroNode3);
doubleLinkList.add(heroNode4);
doubleLinkList.add(heroNode5);
doubleLinkList.add(heroNode6);
doubleLinkList.add(heroNode7);
doubleLinkList.add(heroNode8);
doubleLinkList.add(heroNode9);
doubleLinkList.add(heroNode10);
doubleLinkList.add(heroNode11);
doubleLinkList.add(heroNode12);
System.out.println("原链表的内容:");
doubleLinkList.list();
HeroNode2 heroNode13 = new HeroNode2(7, "宋江", "及时雨~");
doubleLinkList.update(heroNode13);
System.out.println("修改了no=7内容后,现链表的内容:");
doubleLinkList.list();
doubleLinkList.delete(6);
System.out.println("删除了no=6内容后,现链表的内容:");
doubleLinkList.list();
}
}
原链表的内容:
HeroNode [no=1, name=宋江, nickName=及时雨]
HeroNode [no=2, name=吴用, nickName=智多星]
HeroNode [no=3, name=林冲, nickName=豹子头]
HeroNode [no=4, name=宋江, nickName=及时雨]
HeroNode [no=5, name=吴用, nickName=智多星]
HeroNode [no=6, name=林冲, nickName=豹子头]
HeroNode [no=7, name=宋江, nickName=及时雨]
HeroNode [no=8, name=吴用, nickName=智多星]
HeroNode [no=9, name=林冲, nickName=豹子头]
HeroNode [no=10, name=宋江, nickName=及时雨]
HeroNode [no=11, name=吴用, nickName=智多星]
HeroNode [no=12, name=林冲, nickName=豹子头]
节点no为7的节点修改成功!
修改了no=7内容后,现链表的内容:
HeroNode [no=1, name=宋江, nickName=及时雨]
HeroNode [no=2, name=吴用, nickName=智多星]
HeroNode [no=3, name=林冲, nickName=豹子头]
HeroNode [no=4, name=宋江, nickName=及时雨]
HeroNode [no=5, name=吴用, nickName=智多星]
HeroNode [no=6, name=林冲, nickName=豹子头]
HeroNode [no=7, name=宋江, nickName=及时雨~]
HeroNode [no=8, name=吴用, nickName=智多星]
HeroNode [no=9, name=林冲, nickName=豹子头]
HeroNode [no=10, name=宋江, nickName=及时雨]
HeroNode [no=11, name=吴用, nickName=智多星]
HeroNode [no=12, name=林冲, nickName=豹子头]
链表节点no为6的节点成功被删除!
删除了no=6内容后,现链表的内容:
HeroNode [no=1, name=宋江, nickName=及时雨]
HeroNode [no=2, name=吴用, nickName=智多星]
HeroNode [no=3, name=林冲, nickName=豹子头]
HeroNode [no=4, name=宋江, nickName=及时雨]
HeroNode [no=5, name=吴用, nickName=智多星]
HeroNode [no=7, name=宋江, nickName=及时雨~]
HeroNode [no=8, name=吴用, nickName=智多星]
HeroNode [no=9, name=林冲, nickName=豹子头]
HeroNode [no=10, name=宋江, nickName=及时雨]
HeroNode [no=11, name=吴用, nickName=智多星]
HeroNode [no=12, name=林冲, nickName=豹子头]