数据结构(三)【双向链表】

双向链表

在这里插入图片描述

package com.kfm.structure;/*
@CreatTime:2023-09-01   19:16
*/

import java.util.AbstractList;
public class MyDoubleLinked1 extends AbstractList {
    /*
           头节点
        */
    private Node first;


    /*
       尾节点
     */
    private Node last;

    /*
        节点个数
     */
    private int size;

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(Object o) {
        Node c = first;
        while (c != null) {
            if (c.valueEquals(o)) {
                return true;
            }
            c = c.next;
        }
        return false;
    }

    @Override
    public Object[] toArray() {
        Object[] array = new Object[size];
        Node c = first;
        int index = 0;
        while (c != null) {
            array[index] = c.value;
            c = c.next;
            index++;
        }
        return array;
    }

    @Override
    public boolean add(Object o) {
        Node c = new Node(last, o, null);
        if (first == null) {
            first = c;
        } else {
            last.next = c;
        }

        last = c;
        size++;
        return true;
    }

    @Override
    public boolean remove(Object o) {
        // 没有元素
        if (first == null){
            return false;
        }
        if (first.valueEquals(o)){ // 是否是头节点
            // 删除头节点
            Node old = first;
            if (first.next != null){
                first.next.prev = null;
            }

            first = old.next;

            old.next = null;
        } else {
            Node c = first.next;

            while (c != null && !c.valueEquals(o)){
                c = c.next;
            }

            if (c != null){ // 删除元素
                if (last.valueEquals(o)){
                    // 删除最后一个
                    last = last.prev;

                    last.next.prev = null;
                    last.next = null;
                } else {
                    c.prev.next = c.next;
                    c.next.prev = c.prev;

                    c.prev = null;
                    c.next = null;
                }
            }
        }

        size --;
        return true;
    }

    @Override
    public void clear() {
        first = last = null;
        size = 0;
    }

    @Override
    public Object get(int index) {
        Node node = getNode(index);
        return node.value;
    }

    private Node getNode(int index){
        if (index >= 0 && index < size){

            Node point = first; // 头
            if (index >= size / 2){
                point = last; // 尾
                for (int i = 0; i < size - index - 1; i++) {
                    point = point.prev;
                }

            } else {
                // i = 0; i < index
                for (int i = index -1; i >= 0; i--) {
                    point = point.next;
                }
            }

            return point;

        }
        throw new IndexOutOfBoundsException("下标越界: " + index);
    }

    @Override
    public Object set(int index, Object element) {
        if (index >= 0 && index < size) {
            // index 合法
            Node node = getNode(index);
            Object old = node.value;
            node.value = element;
            return old;
        }

        throw new IndexOutOfBoundsException();
    }

    @Override
    public void add(int index, Object element) {
        // TODO checkIndex
        if (index == size){
            add(element);
        } else if (index == 0) {

        } else {

        }
    }

    @Override
    public Object remove(int index) {
        Object old = null;
        if (index == 0){
            // 删头节点
            if (first == last){
                // 只有一个元素
                old = first.value;
                first = last = null;

            } else {
                // 如果有多个
                first = first.next;
                first.prev.next = null;
                old = first.prev.value;
                first.prev = null;
            }
        } else if (index == size - 1) {
            // 删除尾结点
            last = last.prev;

            Node oldLast = last.next;
            oldLast.prev = null;
            old = oldLast.value;
            last.next = null;
        } else {
            Node node = getNode(index);

            node.prev.next = node.next;
            node.next.prev = node.prev;

            node.next = null;
            node.prev = null;

            old = node.value;
        }

        size --;
        return old;
    }

    @Override
    public int indexOf(Object o) {
        Node c = first;
        int index = 0;

        while (c != null){
            if (c.valueEquals(o)){
                return index;
            }
            c = c.next;
            index ++;
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
        Node c = last;
        int index = size - 1;
        while (c != null) {
            if (c.valueEquals(o)) {
                return index;
            }
            c = c.prev;
            index--;
        }
        return -1;

}

    @Override
    public Object[] toArray(Object[] a) {
        Object[] array = new Object[size];
        Node c = first;
        int index = 0;
        while (c != null) {
            array[index] = c.value;
            c = c.next;
            index++;
        }
        return array;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("{");
        Node c = first;
        while (c != null) {
            sb.append(c.value);
            if (c.next != null) {
                sb.append(", ");
            }
            c = c.next;
        }

        sb.append(" }");
        return sb.toString();
    }


    private class Node {
        /*
            前一个节点
         */
        private Node prev;

        /**
         * 值
         */
        private Object value;


        /**
         * 下一个节点
         */
        private Node next;

        public Node(Node prev, Object value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }

        public boolean valueEquals(Object value){
            if (this.value == null){
                return this.value == value;
            } else {
                return this.value.equals(value);
            }
        }
    }
}

测试:

import java.util.Arrays;
public class MyDoubleLinkedTest {
    public static void main(String[] args) {
        MyDoubleLinked1 d = new MyDoubleLinked1();
        /*
        判断链表是否为空
        */
        System.out.println(d.isEmpty());
        /*
        输出链表长度
        */
        System.out.println(d.size());
        /*
        添加链表结点
         */
        d.add("null");
        d.add("a");
        d.add("b");
        d.add("c");
        d.add("d");
        d.add("e");
        d.add("1");
        d.add("2");
        /*
        输出链表
        */
        System.out.println(d);
        /*
        输出链表长度
        */
        System.out.println(d.size());
        /*
        查找指定元素的位置
         */
        System.out.println(d.indexOf("b"));
        /*
        删除指定元素
        */
        d.remove("d");
        System.out.println(d);
        System.out.println(d.size());
        /*
        判断是否包含
        * */
        System.out.println(d.contains("1"));
        System.out.println(Arrays.toString(d.toArray()));
        /*
        获取当前下标的结点
        */
        System.out.println(d.get(4));
        /*
        将当前节点的元素替换
        */
        d.set(3,3);
        System.out.println(d);
        /*
        在指定下标插入结点
        */
        d.add(3,"g");
        System.out.println(d);
        System.out.println(d.size());
        d.remove(3);
        System.out.println(d);
        System.out.println(d.size());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值