Replication of complex linked lists and replication of complex linked lists

Source: Internet
Author: User

Replication of complex linked lists and replication of complex linked lists

Question: there is a complex linked list. Besides a m_pNext pointer pointing to the next node, there is also a m_pSibling pointing to any node or NULL in the linked list. The node C ++ is defined as follows:

 1 struct ComplexNode 2  3 { 4  5     int m_nValue; 6  7     ComplexNode* m_pNext; 8  9     ComplexNode* m_pSibling;10 11 };

Complete the ComplexNode * Clone (ComplexNode * pHead) function to copy a complex linked list.

Train of Thought: in three steps, the time efficiency of O (n) is realized without auxiliary space. Step 1: copy any node N of the original linked list, create a new node n', and then link n' to the end of N.

Step 2: If m_pSibling of node N of the original linked list points to S, m_pSibling of node N of the original linked list points to S's next node'

Step 3: Split the Linked List into two linked lists.

1 # include <stdio. h> 2 # include <iostream> 3 # include "stdafx. h "4 5 struct ComplexListNode 6 {7 int m_nValue; 8 ComplexListNode * m_pNext; 9 ComplexListNode * m_pSibling; 10}; 11 12 ComplexListNode * CreateNode (int nValue ); 13 void BuildNodes (ComplexListNode * pNode, ComplexListNode * pNext, ComplexListNode * pSibling); 14 void PrintList (ComplexListNode * pHead); 15 16 ComplexListNode * CreateNode (int nVa Lue) 17 {18 ComplexListNode * pNode = new ComplexListNode (); 19 20 pNode-> m_nValue = nValue; 21 pNode-> m_pNext = NULL; 22 pNode-> m_pSibling = NULL; 23 24 return pNode; 25} 26 27 void BuildNodes (ComplexListNode * pNode, ComplexListNode * pNext, ComplexListNode * pSibling) 28 {29 if (pNode! = NULL) 30 {31 pNode-> m_pNext = pNext; 32 pNode-> m_pSibling = pSibling; 33} 34} 35 36 void PrintList (ComplexListNode * pHead) 37 {38 ComplexListNode * pNode = pHead; 39 while (pNode! = NULL) 40 {41 printf ("The value of this node is: % d. \ n", pNode-> m_nValue); 42 43 if (pNode-> m_pSibling! = NULL) 44 printf ("The value of its sibling is: % d. \ n ", pNode-> m_pSibling-> m_nValue); 45 else 46 printf (" This node does not have a sibling. \ n "); 47 48 printf (" \ n "); 49 50 pNode = pNode-> m_pNext; 51} 52} 53 54 void CloneNodes (ComplexListNode * pHead ); // copy the original linked list 55 void ConnectSiblingNodes (ComplexListNode * pHead); // set the copied node m_pSibling 56 ComplexListNode * ReconnectNodes (ComplexListNode * pHead); // split Two linked lists 57 58 ComplexListNode * Clone (ComplexListNode * pHead) 59 {60 CloneNodes (pHead); 61 ConnectSiblingNodes (pHead); 62 return ReconnectNodes (pHead ); 63} 64 65 // step 66 void CloneNodes (ComplexListNode * pHead) 67 {68 ComplexListNode * pNode = pHead; 69 while (pNode! = NULL) 70 {71 ComplexListNode * pCloned = new ComplexListNode (); 72 pCloned-> m_nValue = pNode-> m_nValue; 73 pCloned-> m_pNext = pNode-> m_pNext; 74 pCloned-> m_pSibling = NULL; 75 76 pNode-> m_pNext = pCloned; 77 78 pNode = pCloned-> m_pNext; 79} 80} 81 82 // Step 2 83 void ConnectSiblingNodes (ComplexListNode * pHead) 84 {85 ComplexListNode * pNode = pHead; 86 while (pNode! = NULL) 87 {88 ComplexListNode * pCloned = pNode-> m_pNext; 89 if (pNode-> m_pSibling! = NULL) 90 {91 pCloned-> m_pSibling = pNode-> m_pSibling-> m_pNext; 92} 93 94 pNode = pCloned-> m_pNext; 95} 96} 97 98 // step 3 99 ComplexListNode * ReconnectNodes (ComplexListNode * pHead) 100 {101 ComplexListNode * pNode = pHead; 102 ComplexListNode * pClonedHead = NULL; 103 ComplexListNode * pClonedNode = NULL; 104 105 if (pNode! = NULL) 106 {107 pClonedHead = pClonedNode = pNode-> m_pNext; 108 pNode-> m_pNext = pClonedNode-> m_pNext; 109 pNode = pNode-> m_pNext; 110} 111 112 while (pNode! = NULL) 113 {114 pClonedNode-> m_pNext = pNode-> m_pNext; 115 pClonedNode = pClonedNode-> m_pNext; 116 117 pNode-> m_pNext = pClonedNode-> m_pNext; 118 pNode = pNode-> m_pNext; 119} 120 return pClonedHead; 122} 123 124 // ----------------- 125 // \ |/| 126 // |/\/ | \ 128 // -------- + -------- | 129 // ----------------------- 130 131 int main () 132 {133 ComplexListNode * pNode1 = CreateNode (1); 134 ComplexListNode * pNode2 = CreateNode (2); 135 ComplexListNode * pNode3 = CreateNode (3 ); 136 ComplexListNode * pNode4 = CreateNode (4); 137 ComplexListNode * pNode5 = CreateNode (5); 138 139 BuildNodes (pNode1, pNode2, pNode3); 140 BuildNodes (pNode2, pNode3, pNode4); 141 BuildNodes (pNode3, pNode4, NULL); 142 BuildNodes (pNode4, pNode5, pNode2); 143 144 printf ("The original list is: \ n "); 145 PrintList (pNode1); 146 147 ComplexListNode * pClonedHead = Clone (pNode1); 148 149 printf ("The cloned list is: \ n"); 150 PrintList (pClonedHead); 151}

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.