Algorithm question: copying a complex linked list

Source: Internet
Author: User

Note: This article is only for learning and communication. For more information, see the source!

Topic: copy a complex linked list. A complex linked list refers to a node that contains two pointers, one pointing to the next node of a single-chain table and the other pointing to any node in a single-chain table, or the pointer is null.

For convenience, the linked list to be copied is calledPrototype linked listThe copied new linked list is calledCopy linked listTo define the pointer to the next nodeNext pointer, Pointer to other locations is definedAny pointer. Three solutions are provided in sword finger offer: (1) conventional method; (2) Spatial time change method; (3) followed by replication method. The book provides the implementation code for the third method. According to the prompts in the book,Code for the second method.

Before providing the code, we should first understand the algorithm ideas implemented. We know that without the any pointer, we can easily complete the replication task. But with the any pointer, we need to consider how to copy the any pointer of each node. Algorithm 2 sets a one-to-one ing relationship between the replication node and the prototype node during the construction of a single-chain table. After creating a new single-chain table, we can use the any pointer of the prototype node to point to the node location, and query the Any ing table to obtain the any pointer to the replication node, obviously, we can use hash tables or other data structures with ing ideas, such as the red and black trees. In C ++ STL, we can use the map container to implement this ing.

To better understand the replication process, we provide the following complex linked list:


The code for creating a copy linked list is as follows:

# Include <iostream> # include <map> using namespace STD; struct node {int value; node * Next; node * any; // point to a node (int v ): value (V), any (null), next (null) {}};/* Create a linked list, 1-> 2-> 3-> 4-> 5-> 6-> 7 */node * createlist () // create a single-chain table {node * head; node * n1 = new node (1); node * n2 = new node (2); node * N3 = new node (3); node * N4 = new node (4 ); node * N5 = new node (5); node * N6 = new node (6); node * N7 = new node (7); Head = N1; n1-> next = n2; N1-> Any = N3; N2-> next = N3; N2-> Any = N4; N3-> next = N4; N4-> next = N5; N5-> Any = N7; n5-> next = N6; N6-> next = N7; N7-> next = NULL; return head;} void freelist (node * head) // release the linked list space {If (Head = NULL) {return;} else {node * temp = head-> next; Delete head; head = temp; freelist (head) ;}} void visitlist (node * head) // elements in the traversal chain table, recursively traversing {If (! Head) return; node * P = head; while (p) {cout <"(" <p-> value; If (p-> Any) {cout <"," <p-> Any-> value <")" ;}else {cout <", nil )";} if (p-> next) {cout <"->";} p = p-> next;} cout <Endl ;}

The code for copying a complex linked list is as follows:

Node * clone (node * head) // use the hash method to save the any pointer {If (Head = NULL) return NULL; node * P = head; // P is used to scan the source linked list node * head1 = NULL, * P1 = NULL; // head1 points to the copy linked list, and P1 is used to scan the copy linked list Map <node *, node *> m; while (p) // copy the node value and next pointer {If (! Head1) {p1 = new node (p-> value); head1 = p1;} else {p1-> next = new node (p-> value ); p1 = p1-> next;} m [p] = p1; // establishes a ing between the original node and the new node. P = p-> next;} p = head; p1 = head1; while (p) // copy the any pointer of each node in sequence {p1-> Any = m [p-> Any]; P = p-> next; p1 = p1-> next;} return head1 ;}
The test code is as follows:

Int main () {node * head = createlist (); cout <"the output of the original linked list is:"; visitlist (head); node * head1 = clone (head ); cout <"output of the copy linked list:"; visitlist (head1); freelist (head); // release the linked list space return 0 ;}

The test results are as follows:


References ------------ offoffoffer

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.