标签:
pLinkNode CloneComplexLinklist(pLinkNode head) { pLinkNode cur = head->next; if (NULL == head->next) //判断是不是空链表 { return NULL; } pLinkNode newNode= NULL; pLinkNode newhead = NULL; CreateNode(&newhead, cur->data); pLinkNode last= newhead; //新建一个头结点 cur = cur->next; while (NULL != cur) //复制整个链表 { CreateNode(&newNode, cur->data); last->next = newNode; cur = cur->next; last = last->next; } cur = head->next; pLinkNode p = newhead; int s = 0; //计步器 //链接Sibling while (NULL != cur) { last = head->next; while (cur->Sibling&&cur->Sibling != last) //找到当前结点cur的Sibling在旧链表中的位置 { s++; last = last->next; } //如果这个位置不为空 if (NULL != cur->Sibling) { last=newhead; while (s>0) //寻找cur所对应结点p的Sibling在新链表中的位置 { s--; last=last->next; } p->Sibling = last; //链接p的Sibling } cur = cur->next; p = p->next; } return newhead; //返回头结点 }
//复制新的结点链接在原来结点后面 void CopyLinkNode(pLinkNode head) { pLinkNode cur = head->next; pLinkNode newNode = NULL; while (NULL != cur) { CreateNode(&newNode,cur->data); //新建结点newNode newNode->next = cur->next; //将这个结点链接到当前结点的后面 cur->next = newNode; cur = newNode->next; } }
//链接新建结点的Sibling指针 void ConnectSiblingNode(pLinkNode head) { pLinkNode cur = head->next; pLinkNode tail = NULL; while (NULL!=cur) { tail = cur->next; tail->Sibling = cur->Sibling; //tail指针的Sibling指cur的Sibling的位置 if (NULL != tail->Sibling) //如果tail的Sibling指针不空,值链接新节点的Sibling { tail->Sibling = tail->Sibling->next; } cur = tail->next; } }
//拆分两个链表 pLinkNode SeparateLinkList(pLinkNode head) { if (NULL == head->next) { return NULL; } pLinkNode newhead =head->next->next; //指向新链表的的指针 pLinkNode last =newhead; pLinkNode cur = newhead->next; head->next->next = cur; while (NULL!=cur) //拆分这个链表 { last->next = cur->next; last = last->next; cur->next =last->next; cur = cur->next; } return newhead; //返回新链表的地址 }
//复制复杂链表 pLinkNode CloneComplexLinklist(pLinkNode head) { pLinkNode newlist = NULL; CopyLinkNode(head); //复制新的结点链接在原来结点后面 ConnectSiblingNode(head); //链接新建结点的Sibling指针 newlist=SeparateLinkList(head); //拆分链表 return newlist; }
标签:
原文地址:http://blog.csdn.net/lf_2016/article/details/51756586