First, let's look at the structure of the complex list:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7F/23/wKiom1cUoRTwfKgIAABTq3oZVUw569.jpg "title=" 1.jpg " Width= "236" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:236px;height:100px; "alt=" Wkiom1cuortwfkgiaabtq3ozvuw569.jpg "/>
This list cannot be copied directly, and if we copy it directly, we will find that the random of the copied linked list still points to the position of the previous linked list and does not point to a node of its own. So, we need to do a good bit of analysis.
Programme one:
we can replicate one node at a node and place the copied node behind the original node. This forms a linked list: 650) this.width=650; "Src=" http://s5.51cto.com/wyfs02/M01/7F/23/wKiom1cUocHAQFu5AABCJTi2Wzw807.jpg "Title=" 2.jpg "alt=" wkiom1cuochaqfu5aabcjti2wzw807.jpg "/>
then we point the copied node to the original node Random The pointer points to the next position of the position, traversing through the entire list. The next most important thing is to split the list into two separate lists according to the odd couple, and wait until the list is the same as the original linked list.
Scenario Two:
We first let the current position point to head, and then determine whether the head of the random is empty, if the empty direct copy, if not empty, you need to remember the current node, and then traverse backwards, introduce a counter, Go through the counter + +until you find the node that is equal to the random, and then traverse the current position of the linked list that you want to copy in the counter--, backward, and find the current node that will be remembered after the Random points to the found node. You can traverse it sequentially.
if you want to point random to a node of its own, we need to record the random and random points to the node
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/20/wKioL1cUpbHAEJLFAABaqL6hv3w299.png "title=" 3.png " alt= "Wkiol1cupbhaejlfaabaql6hv3w299.png"/>
Compare scenario One with scenario two: we will find that the space complexity of the scheme one is small and the implementation is simple. The space complexity of the scheme two is large, the code is more redundant, but it is easy to think.
Let's take a look at their respective codes:
Programme one:
complexlist* Listcpy2 (complexlist* cl) {node<t>* cur=cl->_head;//copy linked list while (cur) {node<t>* tmp=new Node (cur->_data); tmp->_next=cur->_next;cur->_next=tmp;cur=tmp->_next;} cur=cl->_head;//its randomwhile (cur) {if (cur->_random!=null) {node<t>* next=cur->_next;next->_ Random=cur->_random->_next;cur=cur->_next->_next;}} cur=cl->_head; node<t>* Newhead=null; node<t>* newtail=null;//splits the nodes of the parity position into two lists if (cur)//header and tail nodes {newhead=newtail=cur->_next;cur->_next= Newhead->_next;cur=cur->_next;} while (cur) {newtail->_next=cur->_next;newtail=newtail->_next;cur->_next=newtail->_next;cur=cur- >_next;} return newhead;}
Scenario Two:
complexlist& listcpy (COMPLEXLIST&&NBSP;CL) { node<t>* cur=cl._head; node<t>* cur1=_head;while (cur->_next!=null) {Cur1->_data=cur->_data ; Cur1->_next=cur->_next;if (cur->_random==null) {cur1->_random=null;} else{node<t>* cur4=cur;int count=1;cur4=cur->_next;//Traverse to find the position of the random pointer to the current node of the C1 and use a count// The number of times the traversal is recorded while (CUR->_RANDOM!=CUR4) {if (cur4->_next==null) {cur4->_next=_head;count++;} else {count++;} Cur4=cur4->_next;} By the counter--; Find the pointer position of this current node random, and//assignment Node<t>* cur2=cur1;while (count) {cur2=cur2->_next;if (cur2= =null) {cur2=_head;} count--;} CUR1->_RANDOM=CUR2;} To point the current pointer to the next position cur=cur->_next;cur1=cur1->_next;//in the above code, it is possible to break the next of C1 's last node and point it to //the first node, so the next one is reset to null, or the entire list becomes a // Ring list if (cur->_next==_head) {cur->_next=null;}} Return *this;}
Finally, the blogger is going to come up with a function that points to the position of the random pointer of the linked list node, which is the function that sets the random
void Setrandom (complexlist& cl,int n=-1,int count=0)//The parameter n here indicates that the relative head node has several bits//nodes, that is, the node we want to set. The count represents a relative set of nodes where the position of the relative position of the node is pointed back to//count times and the location of the random is found. {node<t>* Cur1=_head;if (n==-1| | count==0) {return;} while (n--) {cur1=cur1->_next;} node<t>* Cur2=cur1;while (count--) {if (cur2->_next==null) {cur2=_head;} Else{cur2=cur2->_next;}} CUR1->_RANDOM=CUR2;}
This article is from the "11440755" blog, please be sure to keep this source http://luodn.blog.51cto.com/11440755/1765111
Implementing complex linked list replication with C + +