不带头的双向链表

在这里插入图片描述
定义接口IDoubleLinked

public interface IDoubleLinked {
        //头插法
        void addFirst(int data);
        //尾插法
        void addLast(int data);
        //任意位置插入,第一个数据节点为0号下标
        boolean addIndex(int index,int data);
        //查找是否包含关键字key是否在单链表当中
        boolean contains(int key);
        //删除第一次出现关键字为key的节点
        int remove(int key);
        //删除所有值为key的节点
        void removeAllKey(int key);
        //得到单链表的长度
        int getLength();
        void display();
        void clear();
}

定义一个DoubleLinkListImpl类实现IDoubleLinked接口。

public class DoubleLinkListImpl implements IDoubleLinked{
    class Node{
        private int data;//数据
        private Node prev;//前驱
        private Node next;//后继

        public Node(int data){
            this.data=data;
            this.next=null;
            this.prev=null;
        }
    }
    private Node head;
    private Node list;
    public DoubleLinkListImpl(){
        this.head=null;//初始化一下
        this.list=null;//标志尾巴
    }
    @Override
    public void addFirst(int data) {
        //第一次插入
        Node node =new Node(data);
        if (head==null){
            this.head=node;
            this.list=node;
        }else {
            node.next=this.head;
            node.next.prev=node;
            this.head=node;
        }
    }

    @Override
    public void addLast(int data) {
        Node node=new Node(data);
        if (this.head==null){
            this.head=node;
            this.list=node;
        }else {
            this.list.next=node;
            node.prev=this.list;
            this.list=node;
        }
    }

    private  Node searchIndex(int index){
        checkIndex(index);
        Node cur =this.head;
        int count=0;
        while (count<index){
            cur=cur.next;
            count++;
        }
        return cur;
    }
    private void checkIndex(int index){
        if (index<0||index>getLength()){
            throw new IndexOutOfBoundsException("下标不合法");
        }
    }
    @Override
    public boolean addIndex(int index, int data) {
        if (index==0){
            addFirst(data);
            return true;
        }else if (index==getLength()){
            addLast(data);
            return true;
        }else {
            Node node = new Node(data);
            Node cur = searchIndex(index);
            node.next = cur;
            cur.prev.next = node;
            node.prev = cur.prev;
            cur.prev = node;
            return true;
        }
    }

    @Override
    public boolean contains(int key) {
        Node cur =this.head;
        while (cur!=null){
            if (cur.data==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public int remove(int key) {
        Node cur=this.head;
        while (cur!=null){
            if (cur.data==key){
                int oldData=cur.data;
                //删除的节点是头节点
                if (cur==this.head){
                    this.head=this.head.next;
                    this.head.prev=null;
                }else {
                    //删除普通节点
                    cur.prev.next=cur.next;
                    if (cur.next!=null){
                    cur.next.prev=cur.prev;
                    }else {
                        //删除的节点是尾节点,last需要指回来。
                        this.list=cur.prev;
                    }
                }
                return oldData;
            }
            cur=cur.next;
        }
        return -1;
    }

    @Override
    public void removeAllKey(int key) {
        Node cur=this.head;
        while (cur!=null){
            if (cur.data==key){
                //删除的节点是头节点
                if (cur==this.head){
                    this.head=this.head.next;
                    this.head.prev=null;
                }else {
                    //删除普通节点
                    cur.prev.next=cur.next;
                    if (cur.next!=null){
                        cur.next.prev=cur.prev;
                    }else {
                        //删除的节点是尾节点,last需要指回来。
                        this.list=cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public int getLength() {
        int count=0;
        Node cur=this.head;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        Node cur=this.head;
        while (cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    @Override
    public void clear() {
        while (this.head!=null){
            Node cur = this.head.next;
            head.next=null;
            head.prev=null;
            head=cur;
        }
        this.list=null;
    }
}

测试类

public class TestDemo1 {
    public static void main(String[] args) throws InterruptedException {
        DoubleLinkListImpl doubleLinkList=new DoubleLinkListImpl();
        doubleLinkList.addFirst(38);
        doubleLinkList.addFirst(68);
        doubleLinkList.addFirst(98);
        doubleLinkList.display();//98 68 18
        doubleLinkList.addIndex(0,18);
        doubleLinkList.display();// 38 98 68 18
        doubleLinkList.addIndex(2,88);
        doubleLinkList.display();// 38 98 88 68 18
        doubleLinkList.addIndex(4,58);
        doubleLinkList.display();// 38 98 88 68 18 58

        doubleLinkList.remove(38);
        doubleLinkList.display();// 98 88 68 58 18
        doubleLinkList.remove(68);
        doubleLinkList.display();// 98 88 58 18
        doubleLinkList.remove(58);
        doubleLinkList.display();// 98 88 18
        System.out.println(doubleLinkList.remove(188));
        doubleLinkList.removeAllKey(18);
        doubleLinkList.display();
        //doubleLinkList.clear();

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值