#include <iostream>using namespace Std;#define DEFAULT-1structnode{intData Node *next; Node *other;//This is an extra pointer to a complex list that points to a node at a specified location. Node (intD =int()):d ATA (d), Next (NULL), Other (NULL) {}};class contexlist{public:contexlist (): First (NULL) {}voidInsertnext (intA[],intN) {inti =0; Node *p =NULL; for(; i < n; i++) {Node *s = new node (a[i]);if(First = =NULL) {first = s; p = s; }Else{S->next = p->next; P->next = s; p = s; }} p = first; Node *q =NULL; while(P! =NULL) {if(P->next! =NULL) {q = p->next->next; P->other = q;Every two pointers are connected once with the other pointer. //Construct a connection to a complex linked list. } p = p->next; } }voidPrintf ()//Print linked list. {Node *p = first; while(P! =NULL) {if(P->other! =NULL) {cout << p->data <<"->other:"<< p->other->data << Endl; }Else{cout << p->data <<"->other:"<<"NULL"<< Endl;//Print the value of other. } p = p->next; }} FriendvoidCopy (contexlist CL1, contexlist &cl2) {Node *oldptr = CL1. First;if(Oldptr = =NULL)return; Node *newptr = Cl2. First; Node *p = newptr; Node *m =NULL;//Well, in order to prevent duplication and confusion in the back of the connection, I choose //Copy First and then assign value, why do you do this? So organized. //More clearly, I consider moving forward from the back to the copy, which is not possible, because //This is not an array and cannot immediately determine the value of the previous position, if from the previous //The subsequent process and assignment will result in duplicate copies, multiple copies of //situation. while(Oldptr! =NULL) {m = oldptr->next; Node *S1 = new node (); S1->next = m; Oldptr->next = S1; Oldptr = m; }//Assign and point to other to determine. Oldptr = CL1. First; while(Oldptr! =NULL) {m = oldptr->next;if(Oldptr->other! =NULL) M->other = oldptr->other->next; M->data = oldptr->data; Oldptr = m->next; }//Here is a true split reorganization that will CL1 the chain table inside //Copy part of the node is extracted and then connected to the Cl2.first //In the implementation of the split and reorganization, without affecting the previous linked list CL1, //Create a new copy of the linked list with the original linked list. Oldptr = CL1. First; Node *q =NULL; while(Oldptr! =NULL) {m = oldptr->next->next; Q = m;if(p = =NULL) {p = oldptr->next; Newptr = p; Cl2. First= Newptr; }Else{P->next = oldptr->next; p = p->next; } oldptr->next = m; Oldptr = m; }}private:node *first;};intMain () {intA[] = {1,2,3,4,5,6,7,8,9,Ten}; Contexlist CL1; Cl1. InsertnextAsizeof(a)/sizeof(int)); Contexlist Cl2; Copy (CL1, Cl2); cout <<"Linked list 1->cl1:"<< Endl; Cl1. Printf(); cout <<"linked list after copy 2->cl2:"<< Endl; Cl2. Printf();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + depth understanding of complex linked list construction replication