不带头双向链表

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();

}

public class DoubleLinkedImpl implements IDoubleLinked{
    class Node{
        private int data;
        private Node next;//后继
        private Node prev;//前驱
        public Node(int data){
            this.data = data;
            this.next = null;
            this.prev = null;
        }
    }
    private Node head;
    private Node last;
    public DoubleLinkedImpl(){
        this.head = null;
        this.last = null;
    }
    @Override
    public void addFirst(int data) {
    Node node =new Node(data);
    if(head == null){
        this.head = node;
        this.last = node;
    }else {
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
    }
    }

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

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

    @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.last = 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;
                    //cur.next != null表示删除的不是
                    //尾节点
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        //尾节点删除,last需要移动
                        this.last = 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.next != null) {
            Node cur = this.head.next;
            this.head.next = cur.next;
            cur.prev = null;
        }
        this.head = null;
        this.last = null;
    }
}

public class TestMain {
    public static void main(String[] args) {
        DoubleLinkedImpl doubleLinkedList =
                new DoubleLinkedImpl();
        doubleLinkedList.addIndex(0,1994);
        doubleLinkedList.addLast(40);
        doubleLinkedList.addFirst(10);
        doubleLinkedList.addFirst(40);
        doubleLinkedList.addFirst(30);
        doubleLinkedList.addFirst(40);

        doubleLinkedList.addLast(50);
        doubleLinkedList.addLast(60);
        //40 30 20 10 99 50 60
        doubleLinkedList.display();
        doubleLinkedList.addIndex(3,40);
        doubleLinkedList.display();
        doubleLinkedList.addIndex(9,40);
        doubleLinkedList.display();
        System.out.println("=====================");
        doubleLinkedList.clear();
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值