An overview of the

LinkedLIst implements the List interface just like ArrayLIst, but its internal data structure is different. LinkedLIst is implemented based on LinkedLIst (as can be seen from the name), and the random access efficiency is worse than ArrayLIst. Insertions and deletions are more efficient than ArrayList, but they still require traversing parts of the list to move to the subscript position. Only operations at the end of the list, such as add(), addFirest(), removeLast(), and so on, do not move.

LinkedList source code analysis

1. Data structure

LinkedList is implemented based on a LinkedList structure, with header and tail Pointers defined in the class. A bidirectional linked list is maintained internally

 

 

2. Construction method

The default constructor is simple, nothing

 

Add the collection’s elements to the LinkedList:

 

 

 

3. Store

(1) Add (E) Adds elements to the end of the list

 

 

(2)add(int, E) inserts the element at the specified position

 

 

 

(3)addAll(Collection) adds the Collection to the end of the list

 

(4)addAll(int, Collection) adds the Collection to the specified location in the list, which is also described in the constructor

 

AddFirst (E) adds the element to the list header position

 

 

AddLast (E) adds the element to the end of the table

 

 

4. To obtain

 

 

 

5. Remove

Delete method is not given source code, basically similar. There are the following methods:

  1. Remove (): Removes the first element and returns, calling the removeFirst method
  2. Remove (int): Removes the element at the specified position in the list
  3. Remove (Object): Removes the first occurrence of an element from the list
  4. RemoveFirst (): Removes and returns the first element
  5. RemoveLast (): Removes and returns the last element
  6. RemoveFirstOccurrence (Object): Removes the first occurrence of the specified element from the linked list
  7. RemoveLastOccurrence (Object): Removes the last occurrence of the specified element from the list

LinkedList is a powerful class that can be used as a List collection, queue, and stack.