复杂链表的构造与拷贝

来源:互联网 发布:西安79家网络诈骗公司 编辑:程序博客网 时间:2024/05/16 14:59

Q:复杂链表节点定义:

struct ComplexLinkListNode{int value;ComplexLinkListNode *next;ComplexLinkListNode *random;};

next 指向链表中的下一个节点,random指向链表中的任一节点或者为NULL,完成复杂链表的构造和拷贝;

A:

定义数组data为链表依次节点的整数值集合;定义数组randomIndex为链表依次节点的random指针所指节点,其中1代表头结点,依次类推。

/*复杂链表的构造*/
//首先根据data构造普通单链表,random指针置空
ComplexLinkListNode* ComplexLinkListBuild(int data[],int length){<span style="white-space:pre"></span>if(data == NULL){cout<<"Empty Database!"<<endl;return NULL;}<span style="white-space:pre"></span>ComplexLinkListNode *curr = new ComplexLinkListNode();<span style="white-space:pre"></span>ComplexLinkListNode *head = curr;<span style="white-space:pre"></span>curr->value = data[0];<span style="white-space:pre"></span>curr->next = NULL;<span style="white-space:pre"></span>for(int i = 1;i<=length-1;++i)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>ComplexLinkListNode *temp = new ComplexLinkListNode();<span style="white-space:pre"></span>temp->value = data[i];<span style="white-space:pre"></span>temp->next = NULL;<span style="white-space:pre"></span>curr->next =temp;<span style="white-space:pre"></span>curr = curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return head;}
//根据数组<span style="font-family: Arial, Helvetica, sans-serif;">randomIndex重构random指针</span>void ComplexLinkListRandomConstruct(ComplexLinkListNode *head,int randomIndex[],int length){<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>ComplexLinkListNode *randomTemp = head;<span style="white-space:pre"></span>for(int i = 0;i<=length -1;i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(randomIndex[i] == 0){curr->random = NULL;}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>for(int j = 1;j<randomIndex[i];j++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>randomTemp = randomTemp->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>curr->random = randomTemp;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>randomTemp = head;<span style="white-space:pre"></span>curr =curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}
<span style="font-family: Arial, Helvetica, sans-serif;">/*复杂链表的拷贝*/</span>
//拷贝每一个复杂链表节点,并将next指向新拷贝的节点

void ComplexLinkListClone(ComplexLinkListNode *head){<span style="white-space:pre"></span>if(head == NULL)cout<<"Empty Link List!"<<endl;<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>while(curr != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>ComplexLinkListNode *nodeCopy = new ComplexLinkListNode();<span style="white-space:pre"></span>nodeCopy->value = curr->value;<span style="white-space:pre"></span>nodeCopy->next = curr->next;<span style="white-space:pre"></span>nodeCopy->random = NULL;<span style="white-space:pre"></span>curr->next = nodeCopy;<span style="white-space:pre"></span>curr = nodeCopy->next;<span style="white-space:pre"></span>}}//拷贝链表节点的<span style="font-family: Arial, Helvetica, sans-serif;">Random指针</span>void ComplexLinkListRandom(ComplexLinkListNode *head){<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>ComplexLinkListNode *currCopy;<span style="white-space:pre"></span>while(curr)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>currCopy = curr->next;<span style="white-space:pre"></span>if(curr->random){currCopy->random = curr->random->next;}<span style="white-space:pre"></span>curr = currCopy->next;<span style="white-space:pre"></span>}}//将原始复杂链表和拷贝复杂链表分离ComplexLinkListNode* ComplexLinkListSplit(ComplexLinkListNode *head){<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>ComplexLinkListNode *headCopy = head->next;<span style="white-space:pre"></span>ComplexLinkListNode *currCopy = head->next;<span style="white-space:pre"></span>while(curr && currCopy->next)<span style="white-space:pre"></span>{<span style="white-space:pre"></span><span style="white-space:pre"></span>curr->next = currCopy->next;<span style="white-space:pre"></span>currCopy->next = currCopy->next->next;<span style="white-space:pre"></span>curr =curr->next;<span style="white-space:pre"></span>if(curr)currCopy = curr->next;<span style="white-space:pre"></span><span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>curr->next = NULL;<span style="white-space:pre"></span>return headCopy;}
/*复杂链表的遍历*/
//依next域依次遍历复杂链表
void ComplexLinkListDisplay(ComplexLinkListNode *head){<span style="white-space:pre"></span>if(head == NULL)cout<<"Empty Link List!"<<endl;<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>while(curr != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cout<<curr->value<<" ";<span style="white-space:pre"></span>curr = curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>cout<<endl;}
//将复杂链表中节点random指针所指结点依次遍历void ComplexLinkListDisplayByRandom(ComplexLinkListNode *head){<span style="white-space:pre"></span>if(head == NULL)cout<<"Empty Link List!"<<endl;<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>while(curr != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(curr->random == NULL){cout<<"NULL"<<" ";}<span style="white-space:pre"></span>else{cout<<curr->random->value<<" ";}<span style="white-space:pre"></span>curr = curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>cout<<endl;}
/*测试用例*/
void ComplexLinkListCopyTest(){<span style="white-space:pre"></span><span style="white-space:pre"></span>int data[] = {1,2,3,4,5,6,7};<span style="white-space:pre"></span>int randomIndex[] = {2,1,0,0,7,5,6};<span style="white-space:pre"></span>int length = sizeof(data)/sizeof(int);<span style="white-space:pre"></span>ComplexLinkListNode *head = ComplexLinkListBuild(data,length);<span style="white-space:pre"></span>//Complex Link List Constructor<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>ComplexLinkListRandomConstruct(head,randomIndex,length);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>//Complex Link List Copy<span style="white-space:pre"></span>ComplexLinkListClone(head);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>ComplexLinkListRandom(head);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>ComplexLinkListNode *headCopy = ComplexLinkListSplit(head);<span style="white-space:pre"></span>ComplexLinkListDisplay(headCopy);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(headCopy);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);}





0 0
原创粉丝点击