Copy a complex linked list in C Language (detailed illustration)
What is a complex linked list?
A complex linked list refers to a linked list with several nodes. Each node has a data field used to store data, and there are two pointer fields, one pointing to the next node, there is also a random pointing to any node in the current complex linked list or an empty node. What we need to implement today is to generate a new complex linked list for such a complex linked list copy.
The data structure of a complex linked list is as follows:
1 typedef int DataType; // data field type 2 3 // data structure of the complex linked list 4 5 typedef struct ComplexNode 6 7 {8 9 DataType _ data; // data 10 11 struct ComplexNode * _ next; // pointer to the next node 12 13 struct ComplexNode * _ random; // point to a random node (can be any node in the linked list or empty) 14 15} ComplexNode;
As an example of a complex linked list, how can we replicate a complex linked list?
1. First, we should create a new complex linked list based on the existing complex linked list, but the random pointer of all nodes of this new complex linked list points to null, which is well implemented, it is equivalent to creating a simple single-chain table (newlist). The linked list we want to copy may be called oldlist.
2. Next we should merge the newly created complex linked list (newlist) with the existing complex linked list (oldlist) into the following form:
In this case, we have merged two complex linked lists into a linked list (called linklist). By observing this linked list, we can find the merged linked list) the next pointer to the next pointer of the previous pold (oldlist node) of the pnew node in newlist should be the node pointed to by the randow pointer of the pnew node.
In this way, we can keep the pold and pnew pointers backward and finally implement the operation of pointing the random pointer of all newly created complex linked lists (newlist) to the corresponding node. Complex linked lists, such
After completing the preceding steps, we can simply split the linked list into our newlist and oldlist linked lists.
In this way, we have completed the replication of complex linked lists perfectly. The code below is the specific implementation:
Header file complexnode. h:
1 # ifndef _ complex1_node1_h _ 2 3 # define _ complex1_node1_h _ 4 5 6 7 // contains header file 8 9 # include <stdio. h> 10 11 # include <stdlib. h> 12 13 # include <assert. h> 14 15 16 17 18 19 typedef int DataType; // data field type 20 21 22 23 // data structure of the complex linked list 24 25 typedef struct ComplexNode26 27 {28 29 DataType _ data; // data 30 31 struct ComplexNode * _ next; // pointer to the next node 32 33 struct ComplexNode * _ random; // pointer to random node (can be any node in the linked list or empty) 34 35} ComplexNode; 36 37 38 39 // create a complex linked list node 40 41 ComplexNode * BuyComplexNode (DataType x ); 42 43 44 45 // print a complex single-chain table 46 47 void Display (const ComplexNode * cplist ); 48 49 50 51 // copy a complex linked list 52 53 ComplexNode * CopyComplexNode (ComplexNode * cplist); 54 55 56 57 # endif // _ complex1_node1_h __
Complexnode. c
1 # include "complexnode. h "2 3 4 5 // create a complex linked list node 6 7 ComplexNode * BuyComplexNode (DataType x) 8 9 {10 11 ComplexNode * cnode = (ComplexNode *) malloc (sizeof (ComplexNode); 12 13 if (cnode = NULL) // creation failed 14 15 {16 17 perror ("BuyComplexNode (): malloc "); 18 19 return NULL; 20 21} 22 23 // creation successful 24 25 cnode-> _ data = x; 26 27 cnode-> _ next = NULL; 28 29 cnode-> _ random = NULL; 30 31 return cnode; 32 33} 34 35 36 37 // print a complex single-chain table 38 39 void Display (const ComplexNode * cplist) 40 41 {42 43 ComplexNode * pnode = cplist; 44 45 while (pnode) 46 47 {48 49 printf ("% d: % d -->", pnode-> _ data, pnode-> _ random-> _ data ); 50 51 pnode = pnode-> _ next; 52 53} 54 55 printf ("over \ n "); 56 57 58 59} 60 61 62 63 // copy a complex linked list 64 65 ComplexNode * CopyComplexNode (ComplexNode * cplist) 66 67 {68 69 70 71 ComplexNode * pold = NUL L; 72 73 ComplexNode * pnew = NULL; 74 75 ComplexNode * newlist = NULL; // pointer to the header node of the new complex linked list 76 77 pold = cplist; 78 79 // create a new complex linked list 80 81 while (pold! = NULL) 82 83 {84 85 ComplexNode * new_node = BuyComplexNode (pold-> _ data); 86 87 if (newlist = NULL) // when there are no knots in the new complex linked list, 88 89 {90 91 newlist = new_node; 92 93} 94 95 else // when the new complex linked list has knots 96 97 {98 99 ComplexNode * node = newlist; 100 101 while (node-> _ next! = NULL) // find the last node 102 103 {104 105 node = node-> _ next; 106 107} 108 109 node-> _ next = new_node; // Insert a new node 110 111} 112 113 pold = pold-> _ next; 114 115 116 117} // create a new complex linked list end 118 119 120 121 // merge two complex linked lists 122 123 pold = cplist; 124 125 pnew = newlist; 126 127 while (pold) 128 129 {130 131 ComplexNode * curold = NULL; 132 133 ComplexNode * curnew = NULL; 134 135 curold = pold-> _ next; 136 137 curnew = pnew-> _ next; 138 139 if (pold-> _ next = NULL) 140 141 {142 143 pold-> _ next = pnew; 144 145 pold = curold; 146 147 148 pnew = curnew; 149 150 break; 151 152} 153 154 pold-> _ next = pnew; 155 pnew-> _ next = curold; 156 157 pold = curold; 158 159 pnew = curnew; 160 161} // merge two complex linked lists to end 162 163 164 165 // set the random pointer of all nodes on the newly created complex linked list to the corresponding node 166 167 pold = cplist; 168 169 pnew = newlist; 170 171 while (pnew) 172 173 {174 175 pnew-> _ random = pold-> _ random-> _ next; 176 177 pold = pnew-> _ next; 178 179 if (pold = NULL) // This is the pnew _ next pointer already pointing to NULL 180 181 {182 183 break; 184 185} 186 187 pnew = pold-> _ next; 188 189} // end 190 191 192 193 194 // The complicated linked list after separation and merging 195 pold = cplist; 196 197 pnew = newlist; 198 199 while (pold) 200 201 {202 203 ComplexNode * curold = NULL; 204 205 ComplexNode * curnew = NULL; 206 207 if (pnew-> _ next = NULL) // 208 209 210 {211 pold-> _ next = NULL has been separated; 212 213 pnew-> _ next = NULL; 214 215 break; 216 217 218 219} 220 221 curold = pold-> _ next; 222 223 curnew = pnew-> _ next; 224 225 226 227 pold-> _ next = curold; 228 229 pnew-> _ next = curnew; 230 231 pold = curold; 232 233 234 pnew = curnew; 235 236} // separated and merged complex linked list ends 237 238 239 240 return newlist; 241}
Test code:
1 # include "complexnode. h" 2 3 // 4 5 // copy a complex linked list .? Each node of a linked list? Point to next pointer to next? Section 6 7 //, and? A random pointer points? Random node or NULL. Now we need to copy the linked list // realistic, and return the copied new linked list. 10 11 // ps: complex linked list structure 12 13 14 15 16 17 18 19 void test () 20 21 {22 23 ComplexNode * cplist; 24 25 ComplexNode * copylist; 26 27 ComplexNode * node1; 28 29 ComplexNode * node2; 30 31 ComplexNode * node3; 32 33 ComplexNode * node4; 34 35 cplist = BuyComplexNode (1 ); 36 37 node1 = BuyComplexNode (2); 38 39 node2 = BuyComplexNode (3); 40 41 node3 = BuyComplexNode (4); 42 43 node4 = BuyComplexNode (5 ); 44 45 cplist-> _ next = node1; 46 47 node1-> _ next = node2; 48 49 node2-> _ next = node3; 50 51 node3-> _ next = node4; 52 53 cplist-> _ random = node3; 54 55 node1-> _ random = node4; 56 57 node2-> _ random = cplist; 58 59 node3-> _ random = node1; 60 61 node4-> _ random = node2; 62 63 Display (cplist); 64 65 copylist = CopyComplexNode (cplist); 66 67 Display (copylist ); 68 69 70 71} 72 73 int main () 74 75 {76 77 test (); 78 79 return 0; 80 81}
The running result of the program is as follows: