java实现双向链表



public class DoubleLinkList {
    private Node head;  //链表头结点
    private Node tail;   //链表尾结点
    int size=1;
    //初始化链表 
    public DoubleLinkList(int data) {
        // TODO Auto-generated constructor stub
        head=new Node(null, 1, null);
        tail=head;
    }
    //链表是否为空
    public boolean isEmpty() {
        return size==0; 
    }
    //获得双向链表的长度
    public int  getSize() {
        return size;
    }
    //获得指定位置的节点,index从0开始 计算
    public Node getNode(int index) {
        if (index<0||index>size-1) {
            throw new IndexOutOfBoundsException("索引越界");        
        }else {
            int a=0;
            Node current=head;
            while (index!=a) {      
            current=current.next;
            a++;
            }
            //结束循环后current 指向index位置的节点
            return current;
        }           
    }
    //获得指定位置节点的数据
    public int  getData(int index) {
        return getNode(index).data;
    }
    //在链表的头部位置插入节点
    public void  addHeadData(int data) {
        Node node=new Node(null, data, this.head);
         this.head.pre=node;
         this.head=node;
         if (tail==null) {
            this.head=tail;
        }
         size++;
    }
    //在尾部位置插入节点
    public void  addTailData(int data) {
        if (head==null) {
            head=new Node(null, data, null);
            this.tail=this.head;
        }else {
            Node node =new Node(tail, data, null); 
           tail.next=node;
            tail=node;
        }
        size++;

    }
    //在指定位置插入节点
    public void  addData(int index ,int data) {
        if (index<0||index>size) {
            throw new IndexOutOfBoundsException("索引越界");
        }else {
            if (head==null) {
                this.addTailData(data);   //尾部插入
            }else {
                if (index==0) {
                    this.addHeadData(data);
                }else {
                    Node node=this.getNode(index-1);  //要先找到插入的前一个节点
                    Node current=node.next;
                    Node newNode=new Node(node, data, current);
                    //newNode.pre=node,newNode.next=current;
                    node.next=newNode;
                    current.pre=newNode;
                    size++;
                }
            }
        }
    }
    //删除尾结点
    public void  deleteTail() {
        Node node=this.getNode(size-2);
        Node current=node.next;    //要删除的节点
        node.next=current.next;
        current.next=null;
        current.pre=null;      
       size--;
    }
    //删除指定位置节点
    public void delete(int index) {
        //System.out.println(size-1);
        if (index<0||index>size-1)
        throw new IndexOutOfBoundsException("索引越界");
        //删除的是头结点
        if (index==0) {
            Node current=head;
            this.head=head.next;
            this.head.pre=null;
            size--;
        }else {
            Node node=this.getNode(index-1);
            Node current=node.next;    //要删除的节点
            node.next=current.next;
            if (current.next!=null)    //如果是尾结点
                   current.next.pre=node;
            else {
                current.next=null;
                current.pre=null;              
            size--;
            }
        }               
    }
    //正向打印链表
    public void  print() {  
        for(Node node=head;node!=null;node=node.next)
        {
            System.out.print(node.data +"  ");
        }
        System.out.println();
    }
    //反向打印链表
    public void  reversePrint() {
        for (Node node=tail;node!=null;node=node.pre) {
            System.out.print(node.data+"  ");
        }
        System.out.println();
    }
    //链表的结点类
    class Node{
           int data;
           Node pre;
           Node next;
           public Node(Node pre,int data,Node next) {
            // TODO Auto-generated constructor stub
                this.pre=pre;
                this.data=data;
                this.next=next;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DoubleLinkList list=new DoubleLinkList(1);
        for (int i=0;i<6;i++) {
            list.addTailData(i);
        }
        System.out.println("正向打印链表");
        list.print();
        System.out.println("插入头结点后打印链表");
        list.addHeadData(20);
        list.print();
        System.out.println("插入尾结点后打印链表");
        list.addTailData(30);
        list.print();
        list.addData(2, 10);
        System.out.println("插入数据后正向打印链表");
        list.print();
        list.delete(0);
        System.out.println("删除头结点后正向打印链表 ");
        list.print();
        list.deleteTail();
        System.out.println("删除尾结点后正向打印链表 ");
        list.print();
        list.delete(3);
        System.out.println("删除索引为3的数据后打印链表");
        list.print();
        //list.reversePrint();

    }

}
结果

正向打印链表
1 0 1 2 3 4 5
插入头结点后打印链表
20 1 0 1 2 3 4 5
插入尾结点后打印链表
20 1 0 1 2 3 4 5 30
插入数据后正向打印链表
20 1 10 0 1 2 3 4 5 30
删除头结点后正向打印链表
1 10 0 1 2 3 4 5 30
删除尾结点后正向打印链表
1 10 0 1 2 3 4 5
删除索引为3的数据后打印链表
1 10 0 2 3 4 5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wending-Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值