How the "algorithm" 8 linked list and its Sentinels support this elegant data structure

Source: Internet
Author: User

Introduction to three types of linked lists

Forgive my poor drawing ability, spent a long time finally decided to look for these three pictures from the Internet, because the circular linked list of curved arrows is difficult to display perfectly.

The following 3 images are from Wikipedia.

Everyone looks at the picture and should know what kind of list it is. So what is a linked list?

It is the basic data structure with the previous stacks and queues, and the individual objects are arranged in a linear order. You should be aware of the big black dots in the graph, and some of the C + + programming basics will surely be able to guess that the list is pointing to the next object by pointers in each object, and the array is indexed by subscript.

In order to make people feel more impressed, we have come to contact the examples of life.

First of all, one-way linked list (singly linked), my first thought is the following pencil, full of childhood memories! Looking for a long time to find this picture, but do not know its name.

Then a doubly linked list (doublely linked list), the EMU can be very good interpretation of it.

The circular link list (circular linked list) is used in many, small-contact bicycle chains is one of them.

If you have any other examples, please leave them in the comments.

How the linked list is directed

Single linked list

As already mentioned, the linked list points to the next object through a pointer. There is a keyword key and a pointer next in the single-linked list, and of course, there are other satellite data available in the object. We can imagine it this way, in front of the graph is a row, and then in the row in the list node to extend downward, each node is extended into a column, in short, from one dimension to two-dimensional (analogy two-dimensional array).

If you set an element in a list to X, then X.key is its value, and X.next is the successor in the linked list. If X.next=nil, then there is no successor element, so X is the tail of the list (tail).

Doubly linked list

The single-linked list is upgraded to a doubly linked list to consider, nothing more than a precursor, with X.prev to express. Similarly, x.prev=nil, which means that there is no precursor, then X is the head of the linked list. And if the head is empty, then the whole list is empty.

Circular link List

Accordingly, the circular linked list is also promoted by the two-way chain list, that is, the element x of the tail of the list points to the head y of the linked list, the element of the head element y prev to the tail of the list X.

Search, insert, delete of linked list

Search

Our goal is to search for the element in the list L with the first keyword K, and the function will return a pointer to that element.

If it is unfortunate that the element does not exist in the list, then nil is returned.

LIST-SEARCH(L,k)1   x=L.head2   whileand x.key!=k3       x=x.next4   return x

Since this search is linear, in the worst case it will search the entire list, so the List-search run time in this case is Θ(n) 。

Cycle

Next we insert element x (already set keyword key) into the linked list, which is a bit more complicated than searching because it has more to modify. L.head.prev means to go to the head node element of the list, and then take its Prev property.

LIST-INSERT(L,x)1   x.next=L.head2   if L.head!=NIL3       L.head.prev=x4   L.head=x5   x.prev=NIL

It simply inserts an element at the beginning, so time consuming is only Θ(1) 。

Delete

We have a pointer to x, and then we want to remove the X from the list. Specific ideas are also very simple, such as a, B, and C three nodes, if you want to remove the B, you just need to point A's next to C, if it is a double-line linked list also remember to point C prev to a.

list-delete (L,x ) 1  if x  .prev !=nil2  x  .prev  .next  =x   3  else L.head  =x  .next  4  if x  .next !=nil5  x  .next  .prev  =x  .prev 

Since x here is already a pointer, the delete operation requires only Θ ( 1 ) Time, and if the given is not a pointer but a keyword, then call List-search first to search for pointer x, so that time is Θ(n) 。

Sentinel

Today, I suddenly feel that a little more pictures on the blog, even now this "sentinel" image, although not much related to the list, but may be able to help remember it, because the memory is really very important.

Not much nonsense, what is sentinel, what can be done?

Sentinel nodes are often used in linked lists and traversal trees, and do not own or reference any data that is managed by the structure. A sentinel node is often used instead of NULL, which has the following 3 points:

1) Increase the speed of the operation
2) Reduce the complexity of the algorithm and the size of the code
3) Increased robustness of data structures

Supplement: Robustness (robustness) refers to robustness or stability, that is, when something is disturbed, the nature of the thing remains stable. There is an example on the internet where the average value is greatly affected by extreme values, and in this case the median is much more stable.

Add: There is also a Sentinel value definition (also known as a flag value, a signal value, and a dummy value), which is a special value in a particular algorithm that is commonly used to let the condition terminate, so that it is common in the loop and recursion.

In short, sentinels exist to simplify the processing of boundary conditions. Look back to the list of the deletion process, with two if to judge, and use the Sentinel value can not be so troublesome.

LIST-DELETE‘(L,x)1   x.prev.next=x.next2   x.next.prev=x.prev

Since it is Sentinel, then the position of its guard is naturally at the border, for the list, that is between the head and the tail.

The 3 arrows on the top of the picture please fill the brain with an arrow.

Before we had a sentry, we had to access the table header through L.head, and now we can access the table header through L.nil.next.

L.nil is the Sentinel guarding the list territory, then L.nil.prev naturally points to the end of the table, the corresponding L.nil.prev point to the table head.

The above has been modified to delete, the following also take a look at the search and insert.

Search

Compared to the deletion, the original search in the use of the border is not much, here only the first line of L.head replaced with L.nil.next and nil to L.nil can be replaced.

LIST-SEARCH‘(L,k)1   x=L.nil.next2   whileand x.key!=k3       x=x.next4   return x

Insert

As with deletions, boundary judgments are no longer needed!

LIST-INSERT‘(L,x)1   x.next=L.nil.next2   L.nil.next.prev=x3   L.nil.next=x4   x.prev=L.nil

Sentinel's role and precautions

As can be seen from the 3 operations with no Sentinels above, the Sentinel does not reduce the progressive time bounds of the algorithm, but it can reduce the constant factor, such as List-delete ' and List-insert ' both save O ( 1 ) 。 Of course, in some cases, sentinels can reduce the number of more. But it's more about making the code more concise and compact.

However, Sentinel also need to use caution, is the so-called "drug", if there are a lot of short linked list, then give each of the list with a Sentinel is not cost-effective, because the Sentinel to occupy additional storage space, and a short chronology of a lot of time, resulting in a serious waste.

Then this blog will end, and recently in the exam, algorithm series update less, but still thank you for my support!

How the "algorithm" 8 linked list and its Sentinels support this elegant data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.