public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 heroNode5 = new HeroNode2(5, "卡卡5", "赛亚人");
HeroNode2 heroNode1 = new HeroNode2(1, "卡卡1", "赛亚人");
HeroNode2 heroNode3 = new HeroNode2(3, "卡卡3", "赛亚人");
HeroNode2 heroNode4 = new HeroNode2(4, "卡卡4", "赛亚人");
HeroNode2 heroNode2 = new HeroNode2(2, "卡卡2", "赛亚人");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.addByOrder(heroNode5);
doubleLinkedList.addByOrder(heroNode1);
doubleLinkedList.addByOrder(heroNode3);
doubleLinkedList.addByOrder(heroNode4);
doubleLinkedList.addByOrder(heroNode2);
// doubleLinkedList.addByOrder(heroNode1);
// doubleLinkedList.addByOrder(heroNode2);
// doubleLinkedList.addByOrder(heroNode3);
// doubleLinkedList.addByOrder(heroNode4);
// doubleLinkedList.addByOrder(heroNode5);
doubleLinkedList.list();
// doubleLinkedList.add(heroNode1);
// doubleLinkedList.add(heroNode2);
// doubleLinkedList.add(heroNode3);
// doubleLinkedList.add(heroNode4);
// doubleLinkedList.add(heroNode5);
// doubleLinkedList.list();
// HeroNode2 heroNodeUpdate = new HeroNode2(3, "我日", "Shit");
// doubleLinkedList.update(heroNodeUpdate);
// System.out.println();
// doubleLinkedList.list();
//
// doubleLinkedList.delete(heroNode4);
// System.out.println();
// doubleLinkedList.list();
// HeroNode2 heroNodeUpdateA = new HeroNode2(3, "妈的", "Fuck");
// doubleLinkedList.update(heroNodeUpdateA);
// System.out.println();
// doubleLinkedList.list();
//
// doubleLinkedList.delete(heroNode1);
// doubleLinkedList.delete(heroNode2);
// doubleLinkedList.delete(heroNode3);
// doubleLinkedList.delete(heroNode5);
// System.out.println();
// doubleLinkedList.list();
}
}
class DoubleLinkedList{
//初始化头节点,头节点不要动,不存放具体的数据
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
public void addByOrder(HeroNode2 heroNode){
//因为head节点不能动,因此我们需要一个辅助变量来帮助找到插入的位置
HeroNode2 temp = head;
//标志添加的编号是否存在,默认为false
boolean flag = false;
while(true){
//说明temp已经到链表的最后
if(temp.next == null){
break;
}
//位置找到,就在temp的后面插入
if(temp.next.no > heroNode.no){
break;
//说明希望添加的编号已经存在
}else if(temp.next.no == heroNode.no){
//说明编号存在
flag = true;
break;
}else{
//后移,遍历当前列表
temp = temp.next;
}
}
if(flag){
System.out.printf("准备插入的英雄的编号 %d 已经存在了,不能加入
",heroNode.no);
}else{
if(temp.next != null){
if(temp.next.no > heroNode.no){
heroNode.next = temp.next;
temp.next.pre = heroNode;
}
}
temp.next = heroNode;
heroNode.pre = temp;
}
}
/**
* 对于双向链表:
* 1、我们可以直接找到要删除的节点
* 2、找到后,自我删除即可
* @param heroNode
*/
public void delete(HeroNode2 heroNode){
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while(true){
//已经到链表最后节点的next
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.printf("你想删除的编号 [ %d ]不存在
", heroNode.no);
}
}
public void update(HeroNode2 newHeroNode){
if(head.next == null){
System.out.println("链表为空");
return;
}
//根据no编号,找到需要修改的节点
//辅助变量
HeroNode2 temp = head;
boolean flag = false;
while(true){
if(temp == null){
//已经遍历完链表
//已经遍历完链表,也没找到该元素。
break;
}
if(temp.no == newHeroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
}else{
System.out.printf("你想修改的编号 [ %d ]不存在
", newHeroNode.no);
}
}
public void add(HeroNode2 heroNode){
//因为head节点不能动,因此我们需要一个辅助变量
HeroNode2 temp = head;
while(true){
//找到链表的最后
if(temp.next == null){
//temp.next = heroNode;
break;
}
//如果没有找到最后,将temp后移
temp = temp.next;
}
//当退出while循环时,temp就指向了链表的最后
//形成一个双向链表
temp.next = heroNode;
heroNode.pre = temp;
}
//显示链表
public void list(){
//判断链表是否为空
if(head.next == null){
System.out.println("链表为空");
return;
}
//因为head节点不能动,因此我们需要一个辅助变量来遍历
HeroNode2 temp = head;
while(true){
//判断是否到链表最后
if(temp.next == null){
break;
}
//输出节点的信息
System.out.println(temp.next);
temp = temp.next;
}
}
}
class HeroNode2{
public int no;
public String name;
public String nickname;
//指向下一个节点
public HeroNode2 next;
//指向前一个节点
public HeroNode2 pre;
public HeroNode2(int no, String name, String nickname) {
super();
this.no = no;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "HeroNode2 [no=" + no + ", name=" + name + ", nickname="
+ nickname + "]";
}
}