"Data Structures and Algorithms beauty" notes - list

First, what is the list?

1. Like arrays, linked lists is also a linear table .

2. From the memory structure, linked list memory structure is not continuous memory space , a set of memory blocks is fragmented in series, so that a data structure of data stored.

3. Each memory block list is referred to as a node Node. In addition to the node to store data , the need to record a vertical address of the node chain , i.e. subsequent pointer next.

Second, why use a linked list? That list of features

1. Insert, delete data high efficiency O(1) level (just change the pointer to it), a random access low efficiency O(n) levels (required to traverse the chain to the head end of the chain).

2. array and compared to the memory space larger consumption , since data are stored at each node requires additional space to store a successor pointers.

Third, the usual list: single linked list, circular list and doubly linked list

1. Single list

  • Each node contains a pointer only, i.e. successor pointers .

  • There are two special single linked list nodes, i.e., the head node and tail node .

    Why special? Successor list pointer indicates the whole, the tail node to the first node with an empty address null address.

  • Features: The time complexity of insertion and deletion of nodes is O (1), to find the time complexity is O (n).

2. circular list

  • They are consistent with a single linked list in addition to the tail pointer address of the next node to the first node .
  • It applies to stored data cycle characteristics , such as Joseph problems.

3. doubly linked list

  • In addition to data storage nodes, there are two pointers point to the previous node address (pointer precursor prev) and the next node address (pointer successor Next) .

  • Prev pointer precursor and successor pointer of the tail node of the head node point to null address.

  • Features:
    single-chain compared with the same stored data, the need to consume more storage space .
    Insert, delete operations more efficient than a single linked list O (1) level.

    Operation to remove, for example, a delete operation is divided into two kinds: a predetermined data value corresponding to the deleted node and the given node address delete nodes . In the former case, single chain and two-way linked list need to traverse from beginning to end to find the corresponding node to delete, the time complexity is O (n). For the second case, the operation must be performed to find the predecessor node to delete, from start to finish a single traverse the linked list until p-> next = q, the time complexity is O (n), and the doubly linked list can be found immediate predecessor node, time complexity is O (1).

    For an ordered list , doubly linked list by value queries more efficient than single-chain higher . Because we can find the last recorded position p, every query, depending on the size relationship you are looking for value and p, the decision to move forward or backward to find, so look for an average of only half of the data.

4. The two-way circular list

The first node to the predecessor pointers tail node, the subsequent node to the tail pointer is the head node .

Fourth, choose an array or a linked list?

4.1 insert, delete, and random access time of complexity

  • Array: insert, delete time complexity is O (n), a random access time complexity is O (1).
  • List: insert, delete time complexity is O (1), a random access time complexity end is O (n).

4.2 Array shortcomings

  • If the application for a lot of memory space, such as 100M, but if there is no memory space contiguous space 100M, you will fail to apply , even though the available memory space over 100M.
  • Size is fixed, if the lack of storage space, the need for expansion, once the expansion is necessary for data replication, but this time is very time-consuming of.

4.3 shortcoming list

  • Consume more memory space , because of the extra space to store pointer information.
  • List of frequent insertions and deletions, can cause frequent memory allocation and release , likely to cause memory fragmentation, if the Java language, may also cause frequent GC (automatic garbage collector) operation.

4.4 How to choose?

  • Array easy to use, in the realization of a continuous memory space , data buffering can make use of pre-reading group in the CPU, it is more efficient to access
  • List in memory and is not stored contiguously , so the CPU cache unfriendly, no way pre-reading.
    If the code memory usage is very demanding, it is more suitable for an array.

5, how to write elegant code that list? 6 large study skills

5.1 understand the meaning of a pointer or reference

  • Meaning: The one variable (target) is assigned to a pointer (reference), in fact, it is this variable (target) address assigned to the pointer (reference).
  • Example:
    p—>next = q;represents successor node pointer p stored in the memory address of the node q.
    p—>next = p—>next—>next;P represents successor node pointers stored in the memory address of the next node in the node p.

5.2 vigilance pointer loss and memory leaks (single list)

1. Insert node
is inserted between node x and node a node b, b is a next node, p pointer points to the node a, the codes cause loss of pointer and memory leak: p—>next = x; x—>next = p—>next;Obviously, this leads to a subsequent node to the pointer x itself.
This is the correct switching sequence two codes, namely:x—>next = p—>next; p—>next = x;

2. Remove the node
to delete a node b between the node a and node b, b is a next node, p pointer points to the node a:p—>next = p—>next—>next;

5.3 use of "sentinel" simplify difficult to achieve

1. What is the "sentinel"?
The list of "sentinel" node is to solve the border issue , and does not participate in the business logic . If we introduce the "sentinel" node, regardless of whether the list is empty, head pointer will point to this "sentinel" node. We call this list have "sentinel" node of the list referred to take the lead, on the contrary, there is no "sentinel" node list is called the list does not take the lead.

No 2. The introduction of the "sentinel" in
if a node is inserted in the p-node, only two lines of code to get:

new_node—>next = p—>next;
p—>next = new_node;

However, if a node is inserted into the empty list , then the code follows:

if(head == null){
head = new_node;
}

If you want to delete a node p successor node, you can get just one line of code:
p—>next = p—>next—>next;
however, if the delete list of the most one node (the list only this node), the code is as follows:

3. The case of introducing "sentinel" the
"sentinel" node does not store data, regardless of whether the list is empty, the pointer will point to its head, as the head node of the linked list is always present . In this way, insert the first node and other nodes insert, delete, delete the last node and other nodes can implement a unified logic for the same code.

5.4 focus attention boundary conditions

Often used to check whether the correct list of border four boundary conditions:

  • If the list is empty, the code if it works?
  • If the list contains only one node, the code if it works?
  • If the list contains only two nodes, the code if it works?
  • Whether the code logic can work in dealing with head and tail nodes?

5.5 For example drawing, auxiliary thinking

The core idea: the release of brain capacity, leaving more for logical thinking, so it will feel a lot of clear thinking.

Such as inserting data into a single linked list in such an operation, the following drawing:
Here Insert Picture Description

More than 5.6 write more training, there is no shortcut

5 Common list operations:

  • Singly linked list reversal
  • Detecting the list loop
  • Merge two ordered lists
  • Delete list penultimate n nodes
  • Seeking intermediate node list

Welcome attention to micro-channel public number shinerise, slow progress with you ~

Alt

Published 34 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/Shine_rise/article/details/103939274