hsp 双链表 24-26
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
public static void main(String args[]) {
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(6, "林冲", "豹子头");
DoubleLinkedList doublelinkedlist = new DoubleLinkedList();
doublelinkedlist.add(hero1);
doublelinkedlist.add(hero2);
doublelinkedlist.add(hero3);
doublelinkedlist.add(hero4);
doublelinkedlist.list();
HeroNode hero5 = new HeroNode(5, "李逵", "黑旋风");
HeroNode hero6 = new HeroNode(7, "秦明", "霹雳火");
doublelinkedlist.addByOrder(hero2);
doublelinkedlist.addByOrder(hero5);
doublelinkedlist.addByOrder(hero6);
System.out.println("按编号添加后链表的情况~~");
doublelinkedlist.list();
//修改
HeroNode hero7 = new HeroNode(6, "公孙胜", "入云龙");
doublelinkedlist.update(hero7);
System.out.println("修改后链表的情况~~");
doublelinkedlist.list();
// 删除
doublelinkedlist.delete(3);
System.out.println("删除后链表的情况~~");
doublelinkedlist.list();
}
}
class DoubleLinkedList {
private HeroNode head = new HeroNode(0, "", "");
public void add(HeroNode hero) {
HeroNode tmp = head;
while(true) {
if(tmp.next == null) {
break;
}
tmp = tmp.next;
}
tmp.next = hero;
hero.pre = tmp;
}
//根据no将英雄插入到指定位置(如果该no已存在,则添加失败,并给出提示)
public void addByOrder(HeroNode hero) {
//双向链表,我们既可以找插入位置的前一个节点,也可以找插入位置的后一个节点
//下面是找插入位置的前一个节点的代码版本
HeroNode tmp = head;
boolean flag = false; //标志待添加的英雄的no是否存在,默认为不存在
while(true) {
if(tmp.next == null) {
break;
}
if(tmp.next.no > hero.no) {
break;
} else if(tmp.next.no == hero.no) {
flag = true;
break;
}
tmp = tmp.next;
}
if(flag) {
System.out.printf("待插入的英雄的编号%d已存在,插入失败\n", hero.no);
} else {
//一定要注意下面这四条语句的顺序,下面这种顺序用当前的样例看上去是对的,但是链表中pre域的顺序是错的
//第3条语句一定要在第1条和第4条语句之后才是正确的顺序
// hero.next = tmp.next;
// hero.pre = tmp;
// tmp.next = hero;
// tmp.next.pre = hero;
hero.next = tmp.next;
hero.pre = tmp;
//如果待删除的节点是最后一个,就不需要执行下面这条语句,因为此时tmp.next = null,
//null.pre会报空指针异常
//tmp.next.pre = hero;
if(tmp.next != null) {
tmp.next.pre = hero;
}
tmp.next = hero;
}
}
//删除节点
//因为双向链表能够实现自我删除某个节点,所以直接找到那个要删除的节点即可,这点注意和单链表进行区别
public void delete(int no) {
if(head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode tmp = head.next;
boolean flag = false; //标志是否找到待删除节点
while(true) {
if(tmp == null) {
break;
}
if(tmp.no == no) {
flag = true;
break;
}
tmp = tmp.next;
}
if(flag) {
tmp.pre.next = tmp.next;
//如果待删除的节点是最后一个,就不需要执行下面这条语句,因为此时tmp.next = null,
//null.pre会报空指针异常
if(tmp.next != null) {
tmp.next.pre = tmp.pre;
}
} else {
System.out.printf("待删除编号为%d的节点不存在,删除失败\n", no);
}
}
//修改编号和newHero.no相同的节点的信息,no不能改
public void update(HeroNode newHero) {
if(head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode tmp = head.next;
boolean flag = false; //标志是否找到待修改节点
while(true) {
if(tmp == null) {
break;
}
if(tmp.no == newHero.no) {
flag = true;
break;
}
tmp = tmp.next;
}
if(flag) {
tmp.name = newHero.name;
tmp.nickname = newHero.nickname;
} else {
System.out.printf("编号为%d的节点不存在,修改失败\n", newHero.no);
}
}
public void list() {
//判断链表是否为空
if(head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode tmp = head.next;
while(true) {
if(tmp == null) {
break;
}
System.out.println(tmp);
tmp = tmp.next;
}
}
//反转单链表
public void reverseList() {
//如果当前链表为空,或者只有一个节点,则无需反转,直接返回
if(head.next == null || head.next.next == null) {
return;
}
HeroNode reverseHead = new HeroNode(0, "", "");
HeroNode tmp = head.next;
HeroNode target;
//遍历原来的链表,每遍历一个节点,就将其取出并放在新的链表reverseHead的最前端
while(tmp != null) {
target = tmp;
tmp = tmp.next;
target.next = reverseHead.next;
reverseHead.next = target;
}
head = reverseHead;
}
//逆序打印单链表
//方式1:先反转单链表,再遍历即可,但这种方式的缺点是会破坏单链表的结构,有没有不改变单链表的结构,
//又能逆序打印的方法呢?见方式2
//方式2:可以利用栈这个数据结构,利用栈先进后出的特点,将单链表的各个节点压入到栈中,然后逆序出栈,
//就实现了逆序打印的效果
public void reversePrint() {
if(head.next == null) {
System.out.println("链表为空");
return;
}
Stack<HeroNode> stack = new Stack();
HeroNode cur = head.next;
//将单链表的各个节点压入栈中
while(cur != null) {
stack.push(cur);
cur = cur.next;
}
//逆序出栈
while(stack.size() > 0) {
System.out.println(stack.pop());
}
}
}
class HeroNode {
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode pre;
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
public String toString() {
return "HeroNode =" + "[no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}