[Data structure and algorithm] Linked List


1. Getting to know linked lists

Disadvantages of dynamic arrays:

  • May cause a lot of waste of memory space.

How much memory is used?

  • Linked list solution

A linked list is a linear list of chain storage, and the memory addresses of all elements are not necessarily continuous.

Insert picture description here

How to add elements to the list : First create a node object, and allocate a storage space inside the node to store the real data.

Add new elements to allocate new storage space, at this time the memory address is random.

The next node address of the last element is null.

At least two elements in the linked list: size and first

There should be element and next in the first node

element: the stored element
next: the next node
Insert picture description here

2. The inheritance relationship of the linked list

Most of the interfaces of linked lists are consistent with dynamic arrays. Both of them are linear tables.
Linked list interface design

Use the interface method.

The common code
of the two uses inheritance, abstract linear table, to encapsulate.

Let their parent class AbstractList implement the List interface

The parent class reference points to the child class object

Insert picture description here

3. Virtual head node

Sometimes in order to make the code more streamlined and unify the processing logic of all nodes , a virtual head node (not storing data) can be added to the front.[

4. Responsibility for sharing time

Under what circumstances is it appropriate to use amortized complexity?

  • After several consecutive low-complexity situations, some relatively high-complexity situations appear.

5. Reduction of dynamic data

If the memory usage is tight and the dynamic array has more free space, you can consider shrinking operations.

6. Complexity Oscillation

If the expansion multiples and the timing of the expansion are not designed properly, it may cause complexity oscillations.

7. Double Linked List

The previously learned linked list is a singly linked list.
Using a doubly linked list can improve the overall performance of the list.

One-way lists can only be searched from the beginning.
Insert picture description here

How to choose dynamic array and doubly linked list?
Dynamic array: The number of opening and destroying the internal control space is relatively small, but it may cause a waste of memory space (can be solved by shrinking).

Two-way linked list: the number of times of opening and destroying the internal control space is relatively large, but it will not cause a waste of memory space]\

  • If you frequently add and delete operations at the end, dynamic arrays and doubly linked lists can be selected.
  • If you frequently add and delete operations in the header, it is recommended to choose a doubly linked list.
  • If there are frequent (in any position) add and delete operations, it is recommended to use a doubly linked list
  • If there are frequent query operations (random access operations), it is recommended to use dynamic arrays.

With a doubly linked watch, is the single necklace watch useless?
Singly linked list is used in the design of hash table

JAVA's gc root object

JAVA's gc root object, if the object is not referenced by the gc object, the memory will be reclaimed.

  1. The object pointed to by the stack pointer (local variable), stack: local variable
    Example: List list = new ArrayList<>();
    list is the pointer in the stack, the new ArrayList is in the heap space, so the new ArrayList object is It can be called a gc root object.

Determine whether to be recycled: finalize ()

8. One-way circular linked list

Insert picture description here
Insert picture description here

10. Double circular linked list

Insert picture description here

add(), remove() consider the situation

add():

  1. In the case of inserting an element to the last position, the original linked list has only one node
  2. In other positions, insert it to the first node position.

remove():

  1. When there is only one node, size == 0
  2. If there are multiple nodes, delete the first node and the last node

11. Joseph Question

Insert picture description here

Utilize the greatest power of circular linked lists

You can consider adding a member variable, 3 methods
current: point to a node

void reset(): Let current point to the head node first

E next(): Let current go one step backward, that is, current = current.next

E remove(): Delete the node pointed to by current, and let current point to the next node after successful deletion.

12. Static linked list

The linked lists learned above are all implemented by relying on pointers (references).

Linked lists can be simulated through arrays, called static linked lists.

Each element of the array stores 2 data: value, index of the next element

Position 0 of the array stores the information of the head node.
Insert picture description here

Result: 11 -> 44 -> 22 -> 55 -> 33

If each element of the array can only store one data?
Then use two arrays, one for index relations and one for values.

Guess you like

Origin blog.csdn.net/Cirtus/article/details/109293369