/******************* wz asust 20161: First int instance After templating 2: replication can not change the data and structure of the original string 3: the correctness of the random pointer: Apart from the new node, the new and old linked list; also has a high-complexity algorithm, which is to record each node, the node points to the random pointer in the entire chain (queue implementation) after the creation of a new list, according to the queue record, the connection of random pointer; can not record values, only to achieve some special, such as the chain without repeating segments; *******************/#include <map> #include <queue># Include "Wz.h" struct ComplexNode{ int value; ComplexNode* pNext; ComplexNode* pSibling;}; struct myNode{ int value; int* ptr; myNode *next;}; complexnode* clone (Complexnode* phead) { if (pHead == null) &NBSP;&NBSP;RETURN&NBSP;NULL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MAP<COMPLEXNODE*,&NBsp complexnode*> pointmap; complexnode* newhead,*tail; newhead = new complexnode; newhead->value = pHead->value; newHead->pNext = NULL; newhead->psibling = null; pointmap[phead] = newhead; tail = newhead; complexnode *p = phead->pnext; while (p != null) { complexnode* newnode = new ComplexNode; newNode->value = p->value; newNode->pNext = NULL; newnode->psibling = null; tail->pnext = newnode; tail = newnode; pointMap[p] = newNode; p = p->pnext; } p = phead; tail = newhead; while (P!=NULL) { if (P->pSibling!=NULL) { tail->psibling = pointmap.find (p->psibling)->second; } p = p- >pnext; tail = tail->pnext; } return newhead; } void deletelist (Complexnode* phead) { while (phead!=null) { Complexnode* pnext = phead->pnext; delete phead; phead = pnext; } }void print (complexnode* phead) { complexnode* p=phead; while (P) { cout<<p->value<< " "; p=p->pnext; }cout <<endl;} Void print ( mynode* phead) { mynode* p=phead; while (P) { cout<<p->value<< " "; p=p->next; }cout<<endl;} &nbSp; complexnode* myclone2 (Complexnode* phead) { //int k=4; complexnode*p= new complexnode; complexnode*q= null; Complexnode*newhead= null; if (Phead) { p=phead; while (P->pnext) { complexnode*add= new complexnode; add->value=p- >value; add->pSibling=NULL;add->pNext=p->pNext; p->pNext=add; p=p->pNext->pNext; } complexnode*add= new complexnode; add->value =p->value; add->psibling=null;add->pnext=p->pnext; p->pnext=add; p=phead; q=p->pnext; newhead=q; cout<<q->value<<endl; // bug rand Pointor:psibling //while (P->pNext) while (k--) { q->psibling=p->psibling- >pNext; //p->pNext=q->pNext; // q->pNext=p->pNext; //p=p->pNext; // q=q->pNext; } //while (P->pnext) // { // Q->psibling=p->psibling->pnext; // p-> Pnext=p->pnext->pnext; //q->pnext=q->pnext->pnext; // p=p->pNext->pNext; // q=q->pNext->pNext; // } /** made new link:q***/ // newhead=q; // while (Q->pnext) //{ // q->pNext=q->pNext->pNext; // q=q->pnext; // } /*****new link out*****/ // p=pHead; q=newhead; while (Q->pnext) { p->pnext=q->pnext;p=p- >pNext; q->pNext=p->pNext;q=q->pNext; } // delete p->pNext; //can ont do this p->pNext=NULL; // must do this or add 4 to old link // new link out // p=phead; }return newhead;} Void t2 () { cout<< "T2 ()" <<endl; complexnode* p1 = new complexnode; complexnode* p2 = new ComplexNode; ComplexNode* p3 = new ComplexNode; ComplexNode* p4 = new ComplexNode; p1-> Pnext = p2; p2->pnext = p3; p3->p next = p4; p4->pnext = null; p1->value = 1 ; p2->value = 2; p3->value = 3 ; p4-> value = 4; p1->psibling = p3;p2->psibling = p4; p3->pSibling = NULL; p4->pSibling = NULL; print (p1); complexnode* newhead = myclone2 (p1); cout << "Old link:" <<endl; print (p1); cout<< "New link:" << Endl; print (Newhead); // cout<< (newHead->pSibling), Value<<endl; //3 deletelist (Newhead); deletelist (p1); }int main () {t2 (); return 0;} bug at random, not handled.
Next article
650) this.width=650; "Src=" https://set1.mail.qq.com/cgi-bin/viewfile?f= A2bcbf0427c89ddfe9b81df1bf635af413c1a97a98c434db82adcf12983a9da7ac4d93aaa3260c1013d311f6cd1b0e82ecfc4c116a4a91bbcc460d6ca e2dbea25f1da43f0f8552a3931fe7e0949f584180bf89661d8ff635c36789c0200dc878&mailid=zc0513-bifsi0d~d7c9xtviylf~ f64&sid=hgleyao_ppv2kkiv&net=831359242 "style=" border:none;vertical-align:middle;width:693px;height : 159px; "alt=" viewfile?f=a2bcbf0427c89ddfe9b81df1bf635 "/>
650) this.width=650; "Src=" https://set1.mail.qq.com/cgi-bin/viewfile?f= A2bcbf0427c89ddfe9b81df1bf635af413c1a97a98c434db82adcf12983a9da7ac4d93aaa3260c1013d311f6cd1b0e82ecfc4c116a4a91bbdf1101a92 1378caa61e3c9b7192574017068369430b896da8f8d9492855d462950eadd9615c4a1ad&mailid=zc0513-bifsi0d~d7c9xtviylf~ f64&sid=hgleyao_ppv2kkiv&net=831359242 "style=" border:none;vertical-align:middle; "alt=" viewfile?f= a2bcbf0427c89ddfe9b81df1bf635 "/>
Implementation of a complex single-linked list