双向链表:无序添加、有序添加、遍历、反转
package com.example.notes.linkedlist;
import lombok.Data;
/**
* 双向链表
* 17
*
* @date 2022/3/16 - 15:03
*/
public class DoubleLinkedListDemo {
public static void main(String[] args) {
HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 heroNode3 = new HeroNode2(3, "吴勇", "智多星");
HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
SingleLinkedList2 singleLinkedList = new SingleLinkedList2();
singleLinkedList.addByOrder(heroNode4);
singleLinkedList.addByOrder(heroNode1);
singleLinkedList.addByOrder(heroNode3);
singleLinkedList.addByOrder(heroNode2);
singleLinkedList.list();
System.out.println("-------------------------------------------------");
singleLinkedList.reversetList(singleLinkedList.getHead());
singleLinkedList.list();
System.out.println("-------------------------------------------------");
}
}
//定义
@Data
class SingleLinkedList2 {
//初始化头结点
private HeroNode2 head = new HeroNode2(0, "", "");
/*
* 不考虑编号顺序
* */
public void add(HeroNode2 heroNode) {
//因为head节点不能动,需要一个辅助遍历temp
HeroNode2 temp = head;
while (true) {
//找到链表最后
if (temp.next == null) {
break;
}
//如果不是最后则后移
temp = temp.next;
}
temp.next = heroNode;
heroNode.pre = temp;
}
/*
* 考虑编号顺序
* */
public void addByOrder(HeroNode2 heroNode) {
//因为head节点不能动,需要一个辅助遍历temp
HeroNode2 temp = head;
boolean exist = false;
boolean end = false;
while (true) {
//找到链表最后
if (temp.next == null) { //当前为temp链表最后
end = true;
break;
}
if (temp.next.no > heroNode.no) { //找到位置
break;
} else if (temp.no == heroNode.no) {
exist = true;
break;
}
//如果不是正确位置则后移
temp = temp.next;
}
if (exist) {
System.out.println("数据已存在!");
} else {
heroNode.pre = temp;
heroNode.next = temp.next;
if (!end) {
temp.next.pre = heroNode;
}
temp.next = heroNode;
}
}
public void list() {
if (head.next == null) {
System.out.println("链表为空!");
}
HeroNode2 temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.print(temp.getPre());
System.out.print(temp);
System.out.println(temp.getNext());
temp = temp.next;
}
}
/*
* 链表反转
* 22
* */
public static void reversetList(HeroNode2 heroNode) {
//空链表或只有一个数据的链表,无需反转直接返回
if (heroNode.next == null && heroNode.next.next == null) {
return;
}
//定义辅助变量
HeroNode2 cur = heroNode.next; //当前节点,遍历原先链表
HeroNode2 next = null; //当前节点的下一个节点
HeroNode2 temp = null; //当前节点的下一个节点
HeroNode2 reversetHead = new HeroNode2(0, "", "");
while (cur != null) {
boolean end = false;
end = reversetHead.next == null;
next = cur.next;
cur.next = reversetHead.next;
reversetHead.next = cur;
cur.pre = reversetHead;
if (!end) {
cur.next.pre = cur;
}
cur = next;
}
heroNode.next = reversetHead;
}
}
//定义节点
class HeroNode2 {
public int no;
public String name;
public String nickname;
public HeroNode2 pre;
public HeroNode2 next;
public HeroNode2(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
public HeroNode2 getPre() {
return pre;
}
public HeroNode2 getNext() {
return next;
}
//为了显示方便
@Override
public String toString() {
return "heroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname +
'}';
}
}