数据结构与算法Java实现-04双向链表

    科技2026-02-16  18

    Java双向链表

    package aStudy.day2; import jdk.nashorn.internal.objects.NativeError; import java.net.HttpRetryException; import java.sql.SQLOutput; /** * @author haoqi * @Date 2020/9/28 - 7:44 */ public class data02 { public static void main(String[] args) { //测试 System.out.println("双向链表测试"); //创建节点 HeroNode02 hero1 = new HeroNode02(1,"haoqi","heizi"); HeroNode02 hero2 = new HeroNode02(2,"dapeng","pangzi"); HeroNode02 hero3 = new HeroNode02(3,"safei","shouzi"); HeroNode02 hero4 = new HeroNode02(4,"wuzhen","daizi"); //创建双向链表 DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.add(hero1); doubleLinkedList.add(hero2); doubleLinkedList.add(hero3); doubleLinkedList.add(hero4); //输出 doubleLinkedList.list(); //修改 HeroNode02 newheroNode = new HeroNode02(4,"xuchao","shoujian"); doubleLinkedList.update(newheroNode); System.out.println("after the change "); doubleLinkedList.list(); //删除 doubleLinkedList.delete(1); System.out.println("after the delete"); doubleLinkedList.list(); } } //1.定义HeroNode,链表节点 class HeroNode02{ public int no; public String name; public String niloName; public HeroNode02 next; //指向下一个节点 默认null public HeroNode02 pre; //指向前一个节点//默认为null //构造器 public HeroNode02(int no, String name, String niloName) { this.no = no; this.name = name; this.niloName = niloName; } //显示方法,重新toString @Override public String toString() { return "LinkList{" + "no=" + no + ", name='" + name + '\'' + ", niloName='" + niloName + '\'' + '}'; } } // 1.创建一双向链表 class DoubleLinkedList{ //1.初始化头节点,Heronode不存放数据 private HeroNode02 head = new HeroNode02(0,"",""); //返回 public HeroNode02 getHead(){ return head; } //2.遍历双向链表 public void list(){ //判断是否为空 if (head.next == null){ System.out.println("链表为空"); return; } //头节点不能动,建立辅助节点temp遍历 HeroNode02 temp = head.next; while (true){ //判断是否到结尾 if (temp == null) break; //输出节点信息 System.out.println(temp); //后移temp temp = temp.next; } } //2.添加一个节点到双向链表结尾 public void add(HeroNode02 heroNode02){ //利用辅助节点temp HeroNode02 temp = head; while (true){ //判断是否到结尾 if (temp.next == null) break; //后移temp temp = temp.next; } //当退出while循环时,temp指向了结尾 //形成一个循环链表 temp.next = heroNode02; heroNode02.pre = temp; } //3.修改节点的内容,与单项链表节点内容一样 //稍微修改 public void update(HeroNode02 newHeroNode){ //判断是否为空 if (head.next == null){ System.out.println("链表为空~"); return; } //找到要修改的节点,利用no编号 HeroNode02 temp = head.next; boolean flag = false; while (true){ if (temp == null) break; //遍历结束 if (temp.no == newHeroNode.no){ flag = true; //找到了 break; } temp = temp.next; } //利用flag判断是否找到要修改的节点 if (flag){ temp.name = newHeroNode.name; temp.niloName = newHeroNode.niloName; }else System.out.printf("没有找到 %d 的节点,不能修改\n", newHeroNode.no); } //4.从双向链表删除一个节点 //直接找到要删除的节点,进行自我删除 public void delete(int no){ //判断是否为空 if (head.next == null){ System.out.println("链表为空,无法删除"); return; } HeroNode02 temp = head.next; boolean flag = false; while (true){ if (temp == null) break; //遍历结束 if (temp.no == no){ flag = true; //找到了 break; } temp = temp.next; } //判断flag if (flag){ temp.pre.next = temp.next; //注意 //如果是最后一个节点,则不执行下面的语句,否则汇报空指针异常 if (temp.next != null) temp.next.pre = temp.pre; }else System.out.printf("没有找到 %d 的节点,不能修改\n", no); } }
    Processed: 0.014, SQL: 9