> 文档中心 > 双向链表的java实现 详细讲解

双向链表的java实现 详细讲解


链表的介绍

概念

双链表顾名思义,就是链表由单向的链变成了双向链。 使用这种数据结构,我们可以不再拘束于单链表的单向创建于遍历等操作,大大减少了在使用中存在的问题。

基于单链表的优化

1.单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找
2.单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除,单链表删除时节点,总是找到temp,temp是待删除节点的前一个节点(认真体会).

双向链表的图解

双向链表的java实现 详细讲解

双向链表(增删改查)的实现思路

双向链表的java实现 详细讲解

代码实现

package com.qf.linkedList;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,"林冲","豹子头"); DoubleLinkedList doubleLinkedList=new DoubleLinkedList(); //双向链表添加doubleLinkedList.add(heroNode1); doubleLinkedList.add(heroNode2); doubleLinkedList.add(heroNode3); doubleLinkedList.add(heroNode4); doubleLinkedList.list(); //双向链表删除 System.out.println("删除后的数据"); doubleLinkedList.delete(2); doubleLinkedList.list(); //双向链表更新 System.out.println("更新后的数据"); HeroNode2 newNode=new HeroNode2(3,"吴用new","玉麒麟new"); doubleLinkedList.update(newNode); doubleLinkedList.list(); //双向链表添加 System.out.println("顺序添加后的数据"); doubleLinkedList.addOrderly(heroNode1); doubleLinkedList.addOrderly(heroNode3); doubleLinkedList.addOrderly(heroNode4); doubleLinkedList.addOrderly(heroNode2); doubleLinkedList.list();    }}class DoubleLinkedList{    private HeroNode2 hero=new HeroNode2(0,"","");    public DoubleLinkedList(){}    public HeroNode2 getHero() { return hero;    }    //增加节点    public void add(HeroNode2 heroNode){ HeroNode2 temp=hero; while (true){     if (temp.next==null){  break;     }     temp=temp.next; } temp.next=heroNode; heroNode.pre=temp;    }    //按照编号顺序排列    public void addOrderly(HeroNode2 heroNode){ HeroNode2 temp=hero; boolean isExist=false;  //判断该编号是否存在 while (true){     if (temp.next==null){  break;     }else if (temp.next.no>heroNode.no){  break;     }else if (temp.next.no==heroNode.no){  isExist=true;  break;     }     temp=temp.next; } if (isExist){     System.out.println("该编号已存在~~~"+heroNode.no); }else{     heroNode.next=temp.next;     heroNode.pre=temp;     temp.next=heroNode;     if (temp.next!=null){  temp.next.pre=heroNode;     } }    }    //遍历节点    public void list(){ HeroNode2 temp=hero; if (temp.next==null){     System.out.println("未有节点信息~~~");     return; } while (true){     temp=temp.next;     System.out.println(temp);     if (temp.next==null){  break;     } }    }    //更新节点信息    public void update(HeroNode2 newNode){ if (hero.next==null){     System.out.println("节点为空~~~");     return; } boolean isFind=false; HeroNode2 temp=hero; while (true){     if (temp.next==null){  break;     }else if (temp.next.no==newNode.no){  isFind=true;  break;     }     temp=temp.next; } if (isFind){     temp.next.name=newNode.name;     temp.next.nickName=newNode.nickName; }else{     System.out.println("未找到该元素~~~"); }    }    //删除节点信息    public void delete(int no){ if (hero.next==null){     System.out.println("节点为空~~~");     return; } boolean isFind=false; HeroNode2 temp=hero; while (true){     if (temp.next==null){  break;     }else if (temp.no==no){  isFind=true;  break;     }     temp=temp.next; } if (isFind){     temp.pre.next=temp.next;     if (temp.next!=null){  temp.next.pre=temp.pre;     } }else{     System.out.println("未找到该元素~~~"); }    }}class HeroNode2{    public int no;    public String name;    public String nickName;    public HeroNode2 next;    public HeroNode2 pre;    public HeroNode2(){}    public HeroNode2(int no,String name,String nickName){ this.no=no; this.name=name; this.nickName=nickName;    }    @Override    public String toString() { return "HeroNode{" +  "no=" + no +  ", name='" + name + '\'' +  ", nickName='" + nickName  +  '}';    }}

如有不正确的还请指出,留言讨论!!!

双向链表的java实现 详细讲解 创作打卡挑战赛 双向链表的java实现 详细讲解 赢取流量/现金/CSDN周边激励大奖ZDfans