Anatomy of a LinkedList

Time:2022-10-8

LinkedList is another most commonly used member of the List family besides ArrayList. Today, I thoroughly understand LinkedList in this article.

underlying data structure

The bottom layer of LinkedList is a doubly linked list:

transient Node<E> first;
transient Node<E> last;

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;
        }
    }

The data is stored in the item of the Node object, and keeps objects pointing to the previous node and the next node.

Then define the first node first and the last node last for the entire LinkedList to facilitate forward or reverse access to the LinkedList.

Capacity of LinkedList

No array is used to store data, so there is no concept of capacity, and it can be stored indefinitely.

data storage

add(E e)/addlast(E e): Append data to the end of the linked list.
addfirst(E e): Append data to the head of the linked list.
push(E e): push the stack, equivalent to addfirst.
add(int index, E element): Append data to the specified position in the linked list.
addAll(Collection<? extends E> c): Append all the data in the set c to the end of the linked list.
addAll(int index,Collection<? extends E> c): Append all the data in the set c to the specified position of the linked list.

retrieve data

contains(Object o): Determines whether the linked list contains the target object.
peek(): Get the first object of the linked list, and do not remove an object from the linked list (do not stack).
get(int index): Get the specified location object.
pop(): Get the first data of the linked list and pop it from the stack.
removeFirst(): Equivalent to pop.

Since LinkedList is a doubly linked list structure, it implements the Deque interface and provides a series of very convenient queue operation methods. Therefore, if there are similar scenarios such as FIFO and FIFO, etc., LinkedList is the first choice.

Recommended Today

WeexBox Quick Start

overview Weex is a set of mobile cross-platform technical framework developed by Alibaba. The original intention of the research and development is to solve the problems of frequent version release and multi-terminal research and development in the mobile development process. Using the cross-platform technology provided by Weex, developers can easily use Web technology to build […]