首页 > 其他 > 详细

手撕LinkedList

时间:2020-10-21 12:17:04      阅读:24      评论:0      收藏:0      [点我收藏+]
1.数据结构
Doubly-linked list implementation of the {@code List} and {@code Deque}
interfaces.  Implements all optional list operations, and permits all
elements (including {@code null}).
//底层数据结构,为一个双向链表    
private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
这种存储数据,没有扩容的机制
2.查找机制
    /**
     * Returns the (non-null) Node at the specified element index.
     */
    Node<E> node(int index) {
        // assert isElementIndex(index);
		//使用一次二分法,确定获取的数据在上半区还是下半区,进而在半区中循环查找
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }




手撕LinkedList

原文:https://www.cnblogs.com/bigdatalearn/p/cbdecc10f914ed18f08f80d23a2e1e2a.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!