数据结构与算法(六)

双向循环列表代码实测

DoubleNode

package com.bjsxt.queue;

public class DoubleNode {
    //上一个节点
    DoubleNode pre = this;
    //下一个节点
    DoubleNode next = this;
    //节点数据
    int data;

    public DoubleNode(int data){
        this.data = data;
    }

    //增节点
    public void after(DoubleNode node){
        //原来的下一个节点
        DoubleNode nextNext = next;
        //把新节点作为当前节点的下一个节点
        this.next = node;
        //把当前节点做新节点的前一个节点
        node.pre = this;
        //让原来的下一个节点作为新节点的下一个节点
        node.next = nextNext;
        //让原来的下一个节点的上一个节点为新节点
        nextNext.pre = node;

    }

    //下一个节点
    public DoubleNode next(){
        return this.next;
    }

    //上一个节点
    public DoubleNode pre(){
        return this.pre;
    }

    //获取数据
    public int getData(){
        return this.data;
    }

}

TestDoubleNode测试类

package com.bjsxt.queue;

public class TestDoubleNode {

    public static void main(String[] args){
        //创建节点
        DoubleNode n1 = new DoubleNode(1);
        DoubleNode n2 = new DoubleNode(2);
        DoubleNode n3 = new DoubleNode(3);


        //追加节点
        n1.after(n2);
        n2.after(n3);
        //查看上一个,自己,下一个字节的内容

        System.out.println(n2.pre().getData());
        System.out.println(n2.getData());
        System.out.println(n2.next().getData());
        System.out.println(n3.next().getData());
        System.out.println(n1.pre().getData());


    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值