Detailed list of primary

table of Contents

What is a linked list

List of features

How to use a linked list

Basics of the list

Single list

Circular list

        Doubly linked list


What is a linked list

We said earlier, "array", which is a data structure occupy contiguous memory space. But sometimes we need to store large amounts of data, but no decent memory size of contiguous space, it can be how to do it? We have a set of data requires a lot of additions and deletions operation, and to change search operations use less, select the storage array, then how to do the operation does not constitute efficient? List emerged, and it does not require ⼀ block contiguous memory space , it is the "pointer" set remains in the memory block group fragmented Use concatenated .

 

List of features

Why list it came into being? Because the list this structure, just and complementary array of "pros and cons homologous" thinking, since an array of quick change search, slow additions and deletions, complementary additions and deletions to the list should be the fast and slow change search bar. Hey, is this really ha ha ha.

  Array List
Additions and deletions O (n) O (1)
Change search O (1) O (n)

Therefore, frequently check the changed data set storage array with beautiful Zizi; frequent deletions data set, a linked list stored on it pleasant, perfect ~

[Noted here that additions have special steps, simple deletion list is O (1) drop, but to find the position of the element to be added or deleted, the list is indeed O (n) drops ~]

Some readers asked adult friends: CRUD that are more zezheng ah, we can not put forward an array of linked lists come out. Leather! Really put forward an array of linked list to address this situation, the next time we talk.

He said a long time, not to sum up the characteristics of the list does, in fact, the definition also has talked about:

1, the list need not ⼀ block contiguous memory space.          Some people ask, why do not need ah? How to achieve ah? good question! Let's think about it, there is data storage space, we use the time, to access data in the address line, an array The need for continuous, it is because you want to know the address of the first element will be able to infer the back of the data address. If we do not want that data stored in a row, we just know all the addresses of the data on it. But this is too complicated, so we in the space of each data, and all the records about the next memory address space of memory, with a pointer to it. In this way, data is discrete series to get going.

2, deletions fast, slow change search.          And an array of complementary, we have to take the trouble of rubbed his this logic array as continuous, so additions need to move an element, which is traversed is O (n) levels of operation change search calculated directly address one step, which is O (1) operation. List discontinuous Yeah, we direct additions and deletions to change the address pointer on it, How fast, which is O (1) operation, but change search not the same, because we only know the address of the next element, so we only look up slowly start to finish, which is traversed, is O (n) operations.

3, scalability, without specifying the size of the advance . And another array of complementary features to the array is not flexible, you need to specify the size of the advance.

4, memory utilization is below the array.       Linked list of nodes, not only need to store data, but also store the next address data, store the same data, the required memory certainly does not need compared to an array of memory addresses to be bigger.

 

How to use a linked list

The list is divided into many, let's start with the simplest and most basic single list to begin with.

Basics of the list

In fact, there is room for refinement of the first picture of the article, let's give him a refined look.

Compared to an array, a linked list of the next multi-structure, is used to store a position of the lower node. For purposes of the list, i.e., a memory block is a node.

In the list, the more specific is the head node and tail node, the first node is normally stored base address list, and the next node to the end of the value null.

 public class ListNode {  //链表的一个结点
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
  }

Single list

FIG first article is a single chain structure, where the "single" refers to a single direction -

Basic Operation list is CRUD. Let's look at how to achieve

By (interpolation) : Since the node list memory blocks are scattered, it is necessary only to address elements of the element A to point C needs to add, point to addresses A and C originally directed to.

After insertion:

C.next=A.next;
A.next=C;

Delete: the element directly to the element A to an element B C pointed.

A.next=A.next.next;

Change, check

Circumstances change search rather special, because the list of memory is discrete, so we do not like an array position "random access", node, it only points to other nodes know, so we want to change a check node must be honest begin looking from the head of the list, which is a traversing operation, requires time complexity O (n) a.

The figure represents a step trying to find data to find 3 =.

 

Circular list

Circular list is a special single-chain, specifically in particular: his next pointer of the tail node, pointing to the first node, it becomes so long.

Single list and phase ratios, the advantages of the circular list from the head end of the chain to chain more ⽅ ⽐ it. When the data to be processed having a ring structure characteristics, particularly suitable are recorded using a circular linked list. 

 

 

 

Basic CRUD achieved, then ushered in the magical needs: delete a given node?

Not that the above mentioned advantages delete nodes Mody, O (1) complexity, where the list.

no no no, say above are theoretical things, we carefully caressed the deletion of specific details.

 

Delete but two cases:

1, delete node is equal to a given value

2, to remove a given node pointer

 

In the first case, the need to have a process of deleting looking for, simply delete operation is indeed O (1), but the lookup process is O (n) a.

In the second case, we already know the node to be deleted, but you want to delete this node, needed his predecessor nodes in a single linked list structure, we have to traverse to find Han Han's predecessor node a.

 

Ah ~ sounds good trouble ah, a single kind of demand and very common. So we can not think of a single list to modify the structure of it? Now delete nodes need predecessor node, then we add one more pointer to the predecessor node thousand million.

Thus came into being doubly linked list.

Doubly linked list

​
 public class ListNode {  //链表的一个结点
     int val;
     ListNode next;
     ListNode pre;
     ListNode(int x) { val = x; }
  }

Doubly linked list compared to a single linked list, he can hold ⽀ case complexity of O (1) time to find the predecessor node.

So, for the second shot kind deletion situation, the complexity of a single list delete operation requires O (n) time, ⽽ doubly linked list just in time complexity O (1) is to get! 

Similarly, if we want before a specified node list of ⾯ Insert ⼀ nodes, doubly linked list ⽐ single list there are zoomed advantage. Doubly linked list can be (1) get the time complexity O, ⽽ way linked list requires complexity O (n) time.

For ⼀ ordered list, the efficiency of a doubly linked list by value queries should ⽐ single list ADVANCED ⼀ more. Because the last time to find the location of p we can record each time a query, according zoomed ⼩ relationship To find the value of p, the decision to move forward or backward to find, so on average only need to look halfway data.
 

 

And so on, we found that two-way circulation list and the list is not contradictory, which means you can come up with a way circular linked list.

 

In the actual software development, not just profit Using complex analysis which decided Using data structures to store data.
To the list of arrays and metered for various performance ratios, which ⼀ integrated to select the Use both in one.
 

Published 38 original articles · won praise 6 · views 1893

Guess you like

Origin blog.csdn.net/weixin_43827227/article/details/105012637