Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the LinkedList query method

Shulou Source: shulou.com Published: 2022-06-01 11:36:30 09月14日 Update

This article mainly explains "how to use LinkedList query method". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the LinkedList query method.

The bottom layer of LinkedList is based on two-way linked list, and it also implements the List interface, so it also has some characteristics of List (JDK1.7/8 cancels the loop and modifies it to two-way linked list).

New method public boolean add (E e) {

LinkLast (e)

Return true

}

/ * *

* Links e as last element.

, /

Void linkLast (E e) {

Final Node l = last

Final Node newNode = newNode (l, e, null)

Last = newNode

If (l = = null)

First = newNode

Else

L.next = newNode

Size++

ModCount++

}

It can be seen that each insert is a move pointer, which is much more efficient than the copy array of ArrayList.

Query method public E get (int index) {

CheckElementIndex (index)

Return node (index). Item

}

Node node (int index) {

/ / assert isElementIndex (index)

If (index

< (size >

> 1) {

Node x = first

For (int I = 0; I

< index; i++) x = x.next; return x; } else { Node x = last; for (int i = size - 1; i >

Index; iMurt -)

X = x.prev

Return x

}

}

The above code takes advantage of the characteristics of the two-way linked list. If the index is close to the link header, it traverses from the node header. Otherwise, the traversal starts at the end of the node. Use space (two-way linked list) in exchange for time.

Node () will get a node with the performance of O (nbind 2). If the index value is more than half the size of the linked list, it will traverse from the end node.

This efficiency is very low, especially when the index is closer to the median value of size.

At this point, I believe you have a deeper understanding of "how to use the LinkedList query method". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Method query bidirectional content efficiency node node learning practical deeper code interest size header utility practical tail underlying performance copy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi NVidia MariaDB macOS