java实现双向链表

之前分析了单链表的增删改查操作,双链表操作只是在此基础上稍微修改一下,可以作图辅助分析,这里直接上完整的代码。测试某个函数功能直接调用就行。新加了一个findforwardnode函数可以查找当前节点的前一个节点,可以测试是否是双向链表。

package com.linear.test;

/**
 * @author ymm
 * @create 2020-10-05 19:31
 */
public class DoubleLinkList {
    public static void main(String []args)
    {
        Doublelist list = new Doublelist();
        HeroNode2 link1=new HeroNode2(1,"宋江","及时雨");
        HeroNode2 link2=new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 link3=new HeroNode2(3,"吴用","智多星");
        HeroNode2 link4=new HeroNode2(4,"林冲","豹子头");
        HeroNode2 link5=new HeroNode2(1,"lingchong","baozitou");
        list.AddByOrder(link1);
        list.AddByOrder(link4);
        list.AddByOrder(link3);
        list.AddByOrder(link2);
        //list.update(link5);
        list.travel();

    }
}


class Doublelist{
    HeroNode2 head = new HeroNode2(0,"","");
    //尾插链表,测试成功不报错
    public void AddInTail(HeroNode2 newNode)
    {
        HeroNode2 temp=head;
        while(temp.next!=null)
        {
            if(temp.heronum== newNode.heronum)
            {
                System.out.println("节点已经存在。不可重复插入\n");
                return;
            }
            temp=temp.next;
        }
        temp.next=newNode;
        newNode.pre=temp;
        newNode.next=null;
    }
    //头插双链表,查找重复插入还是要遍历链表
    public void AddInHead(HeroNode2 newNode)
    {
        if(head.next==null) //插入第一个节点时
        {
            head.next=newNode;
            newNode.pre=head;
        }
        //不只一个节点时
        else
        {
            newNode.next=head.next;
            head.next.pre=newNode;
            head.next=newNode;
            newNode.pre=head;
        }

    }
    //编号顺序插入,不同于单链表,可以直接定位到带插入位置处,因为可以双向链表可以找到前一个节点
    public void AddByOrder(HeroNode2 newNode)
    {
        HeroNode2 temp=head;
        boolean flag=false;
        while(true)
        {
            //这里如果是1>4,现在插入3就不对,因为满足第一条件,还是要找前一个节点辅助
            if(temp.next==null)   //遍历到头就尾插
            {
               temp.next=newNode;
               newNode.pre=temp;
               flag=true;
            }
            if(temp.next.heronum> newNode.heronum)   //找到位置节点,在节点之前的位置插入就可,和单链表相似就是没想出来
            {
                newNode.next=temp.next;
                temp.next.pre=newNode;
                temp.next=newNode;
                newNode.pre=temp;
                flag=true;
            }
            if(temp.heronum==newNode.heronum)
            {
                System.out.print("节点已经存在,不可插入\n");
            }
            temp=temp.next;
            if(flag)
                return;
        }
    }


    //测试一下是否是双链表,测试成功,可以找到前一个节点
    public void findforwardnode(int number)
    {
        HeroNode2 temp = head.next;
        while(temp!=null)
        {
            if(temp.heronum==number)
            {
                System.out.print("前一个节点为");
                System.out.println(temp.pre);
                return ;
            }
            temp=temp.next;
        }
        System.out.print("找不到对应的节点\n");
    }

    //遍历链表
    public void travel() {
        if (head.next == null) {
            System.out.print("链表为空\n");
            return;
        }
        else {
            HeroNode2 temp = head.next;
            while (temp != null) {
                System.out.println(temp);
                temp = temp.next;
            }
        }
    }
    //按照编号删除节点,测试成功
    public void delete(int number)
    {
        HeroNode2 temp=head;
        boolean flag=false;
        while(temp!=null)
        {
            if(temp.heronum==number)
            {
                if(temp.next==null)  //最后一个节点比较特殊
                temp.pre.next=temp.next;
                else
                {
                    temp.pre.next=temp.next;
                    temp.next.pre=temp.pre;
                }
                flag=true;
            }
            temp=temp.next;
            if(flag)
                return ;
        }
        System.out.print("删除节点编号不存在\n");
        return ;
    }

    //按序号更新信息,和删除类似,编号不能更改
    public void update(HeroNode2 newNode)
    {
        HeroNode2 temp = head.next;
        while(true)
        {
            if(temp==null)
            {
                System.out.print("待更新的节点找不到\n");
                return ;
            }
            if(temp.heronum==newNode.heronum)
            {
                temp.heroname=newNode.heroname;
                temp.heroNickName=newNode.heroNickName;
                return;
            }
            temp=temp.next;
        }
    }

}




//定义双链表节点结构
class HeroNode2{
    int heronum;
    String heroname;
    String heroNickName;
    HeroNode2 next;
    HeroNode2 pre;
    public HeroNode2(int heronum,String heroname,String hreoNickName)
    {
        this.heronum=heronum;
        this.heroname=heroname;
        this.heroNickName=hreoNickName;
    }
    @Override
    public String toString() {
        return " [heronum=" + heronum + ", heroname=" + heroname + ", heroNickName=" + heroNickName + "]";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值