您好,登錄后才能下訂單哦!
/******************* WZ ASUST 2016 1:先int實例 后模板化 2: 復制不能改變原串的數據及結構 3: 隨機指針的正確性 思考:除了追加新結點后分離新舊鏈表; 還有一復雜度高的算法,就是記錄下每一個結點,隨機指針指向的結點在整個鏈中的排序(隊列實現)建立新鏈表后,根據隊列記錄,連接隨機指針; 不能記錄值,僅能實現一些特殊的,如無重復段的鏈; *******************/ #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) return NULL; map<ComplexNode*, 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; } 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->pNext = 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 沒處理
下一篇
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。