实现一个自己版本的 linkedList

在这个变量当中实现了一个双向链表

public class Node {
    int val;
    Node prev;
    Node next;


    public Node(int val) {
        this.val = val;
    }
    @Override
    public String toString() {
        return "[" + val + "]";
    }
}


public class MyLinkedList {
    private Node head;
    private Node tail;
    private int length;

    public MyLinkedList() {
        head = null;
        tail = null;
    }

    public MyLinkedList(int length) {
        this.length = length;
    }

    //插入
    public void addFirst(int val) {
        Node newNode = new Node(val);
        if (head == null) {
            head = newNode;
            tail = newNode;
            length++;
            return;
        }
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
        length++;
    }

    public void addLast(int val) {
        Node newNode = new Node(val);
        if (head == null) {
            head = newNode;
            tail = newNode;
            length++;
            return;
        }
        tail.next = newNode;
        newNode.prev = tail;
        tail = tail.next;
        length++;
    }

    public void add(int index, int val) {
        if (index < 0 || index > length) {
            return;
        }
        if (index == 0) {
            addFirst(val);
            return;
        }
        if (index == length) {
            addLast(val);
            return;
        }
        Node newNode = new Node(val);
        Node nextNode = getNode(index);
        Node prevNode = nextNode.prev;

        newNode.prev = prevNode;
        newNode.next = nextNode;
        prevNode.next = newNode;
        nextNode.prev = newNode;
        length++;

    }

    public void removeFirst() {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            head = null;
            tail = null;
            length--;
            return;
        }
        Node nextNode = head.next;
        nextNode.prev = null;
        head = nextNode;
        length--;
    }

    public void removeLast() {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            head = null;
            tail = null;
            length--;
            return;
        }
        Node prevNode = tail.prev;
        prevNode.next = null;
        tail = prevNode;
        length--;
    }

    public void removeByIndex(int index) {
        if (index < 0 || index >= length) {
            return;
        }
        if (index == 0) {
            removeFirst();
            return;
        }
        if (index == length - 1) {
            removeLast();
            return;
        }
        Node curNode = getNode(index);
        Node prevNode = curNode.prev;
        Node nextNode = curNode.next;

        prevNode.next = nextNode;
        nextNode.prev = prevNode;
        length--;
    }

    public void removeByValue(int val) {
        int index = indexOf(val);
        if (index == -1) {
            return;
        }
        removeByIndex(index);
    }

    //查找
    public int indexOf(int val) {
        Node cur = head;
        for (int i = 0; i < length; i++) {
            if (cur.val == val) {
                return i;
            }
            cur =cur.next;
        }
        return -1;
    }

    // 根据下标找元素
    public Node getNode(int index) {
        if (index < 0 || index >= length) {
            return null;
        }
        Node cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public int get(int index) {
        if (index < 0 || index >= length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return getNode(index).val;
    }

    public void set(int index, int val) {
        if (index < 0 || index >= length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        Node node = getNode(index);
        node.val = val;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值