整体代码:
/*
* 项目名称:ANA
* 文件名称:DoubleLickedListDemo.java
* Date:2023/9/25 上午8:46
* Author:yan_Bingo
*/
package Learning;
/**
* @author Yan_Bingo
* @version 1.0
* Create by 2023/9/25 08:46
*/
public class DoubleLickedListDemo {
public static void main(String[] args) {
DoubleLickedList doubleLickedList = new DoubleLickedList();
HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
doubleLickedList.addByorder(heroNode1);
doubleLickedList.addByorder(heroNode3);
doubleLickedList.addByorder(heroNode2);
doubleLickedList.addByorder(heroNode4);
System.out.println("展示链表的情况");
doubleLickedList.showLickedList();
}
}
class DoubleLickedList{
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
//加入链表
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 addByorder(HeroNode2 heroNode){
HeroNode2 temp = head;
boolean flag = false; // 标志要加入的英雄编号是否已经存在
boolean flag2 = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.no == heroNode.no){
flag = true; // 说明要加入的英雄的编号已经存在
break;
}else if(temp.next.no > heroNode.no){
flag2 = true;
break;
}
temp = temp.next;
}
if(flag){
System.out.println("要加入的英雄的编号已经存在~~~");
return;
}else {
if(flag2){
heroNode.next = temp.next;
temp.next.pre = heroNode;
heroNode.pre = temp;
temp.next = heroNode;
}else{
temp.next = heroNode;
heroNode.pre = temp;
}
}
}
// 修改链表节点,根据 no 来修改
public void updateNode(HeroNode2 heroNode){
if(head.next == null){
System.out.println("当前链表为空,无法修改~~");
return;
}
HeroNode2 temp = head;
boolean flag = false;
while (true){
// 遍历到了尾结点
if(temp == null) break;
if (temp.no == heroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.name = heroNode.name;
temp.nickName = heroNode.nickName;
}else{
System.out.printf("未找到编号为 %d 的节点,无法修改", temp.no);
System.out.println();
}
}
// 删除链表的某一个节点
public void deleteNode(int no){
if(head.next == null){
System.out.println("当前链表为空,无法删除~~");
return;
}
HeroNode2 temp = head.next;
boolean flag = false; // 标志是否找到需要删除的节点
while(true){
if(temp.next == null){
break;
}
if (temp.no == 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.printf("未找到编号为 %d 的节点,无法删除", no);
}
System.out.println();
}
// 遍历链表
public void showLickedList(){
// 先判断当前链表是否为空
if(head.next == null){
System.out.println("当前链表为空~~~");
return;
}
HeroNode2 temp = head.next;
while (temp != null){
System.out.println(temp);
temp = temp.next;
}
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 "HeroNode2{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}