Analysis on two-way linked list

Source: Internet
Author: User

Let's talk to you:
1. insert)
(1) The basic principle is that the dual-chain table is like a row of people standing Hand in Hand. Each person's right hand (next) pulls the next person, and the right hand (prior) pulls the previous person, every two people have two hands interconnected. the insert operation is actually to add a person to the team. He needs to pull the people on both sides of the team, that is, there is a relationship between three people, because each two people have two hands interconnected, therefore, four operations are required. if a node is known
Q, p, need to insert s, called prototype ).
----------------------------------
Q-> next
--->
Q p
<---
P-> prior
------------------------------- Assume that the node s needs to be inserted, perform the following four operations :-----------------------------------------
(1) (2) Complete the handshake between q and s.
(1) q-> next = s;
(2) s-> prior = q; q-> next
--->
Q s
<---
S-> prior -----------------------------------------
(3) (4) Complete the handshake between s and p.

(3) s-> next = p;
(4) p-> prior = s; q-> next s-> next
--->
Q s p
<---
S-> prior p-> prior
----------------------------------------- In this article, the prototype is a variant of the prototype. Only the node p is known, but node q is unknown. However, we can obtain the following relationship through the nature of the two-way linked list (before insertion): p-> prior = q; therefore, we can replace q with p-> prior for the above four operations:
(1) p-> prior-> next = s;
(2) s-> prior = p-> prior; file: // (1) (2) Complete the handshake between q and s.
(3) s-> next = p;
(4) p-> prior = s; file: // (3) (4) Complete the handshake between s and p.
(2) The q node is not known, so p-> prior should be used instead. Therefore, the value of p-> prior should be changed when q nodes are no longer used, prototype: (1) q-> next = s;
(2) s-> prior = q;
(3) s-> next = p;
(4) p-> prior = s;
(4) changed the value of p-> prior, while (1) (2) q is required, so (4) it should be after (1) (2.
Ii. insert)
(1) Basic Principles
The delete operation is like a person exiting the team, but before exiting the team, he needs to put his hands on both sides to maintain the continuity of the team. It also seems that the work should be handed over before leaving the company, it's like I am now, haha ..., if q, s, p are known and s needs to be deleted, the following two operations are required because only two people are involved: (1) q-> next = p;
(2) p-> prior = q; q-> next s-> next
--->
Q s p
<---
S-> prior p-> prior
Q-> next
--->
Q p
<---
P-> prior is a variant of the prototype above. Only s is known. However, we can introduce the following relationship from the two-way linked list: q = s-> prior;
P = s-> next; therefore, you can replace (1) s-> prior-> next = s-> next;
(2) s-> next-> prior = s-> prior; because s, and prior and next values are not changed in the two operations, (1) (2) The order is irrelevant. iii. Conclusion 1) for the insertion of a two-way linked list, you only need to know the Insertion Location of a node to complete the insertion;
2) to delete a two-way linked list, you only need to know how to delete the node. Therefore, for the insertion and deletion of a two-way linked list, the time is O (1 ). For personal comments, we welcome your criticism and correction.

Related Article

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.