题目:实现复杂链表的复制。在复杂链表中,每个结点除了有一个next指针指向下一个结点之外,还有一个sibling指向链表中的任意结点或者NULL。
复杂链表示意图:
思路:
可以分成三步:
1.根据原始链表的每个结点N创建对应的N',并将N'放到N的后面
2.设置复制出来的结点的sibling指针假设P为原始链表的某结点,则复制链表的对应结点p'->sibling就等于p-
>sibling->next
3.把第二步得到的链表拆分成两个链表,奇数位为原始链表,偶数位为复制链表
代码:
/*
复杂链表的复制
by Rowandjj
2014/8/6
*/
#include<iostream>
using namespace std;
typedef struct _NODE_
{
int data;
struct _NODE_ *next;
struct _NODE_ *sibling;
}Node,*pNode;
//------------------------------------
//第一步:根据原始链表的每个结点N创建对应的N',并将N'放到N的后面
void CloneNodes(pNode pHead)
{
pNode pTemp = pHead;
while(pTemp != NULL)
{
pNode pNew = (pNode)malloc(sizeof(Node));
if(!pNew)
{
exit(-1);
}
pNew->data = pTemp->data;
pNew->next = pTemp->next;
pNew->sibling = NULL;
pTemp->next = pNew;
pTemp = pNew->next;
}
}
//第二步:设置复制出来的结点的sibling指针
//假设P为原始链表的某结点,则复制链表的对应结点p'->sibling就等于p->sibling->next
void ConnectSiblingNodes(pNode pHead)
{
pNode pTemp = pHead;
while(pTemp != NULL)
{
pNode pClone = pTemp->next;
if(pTemp->sibling != NULL)
{
pClone->sibling = pTemp->sibling->next;
}
pTemp = pClone->next;
}
}
//第三步:把第二步得到的链表拆分成两个链表,奇数位为原始链表,偶数位为复制链表
pNode ReconnectNodes(pNode pHead)
{
pNode pTemp = pHead;
pNode pCloneHead = NULL;//克隆链表的头结点
pNode pCloneNode = NULL;
if(pTemp != NULL)
{
pCloneHead = pCloneNode =pTemp->next;//定位克隆链表的头结点
pTemp->next = pCloneHead->next;
pTemp = pTemp->next;
}
while(pTemp != NULL)
{
pCloneNode->next = pTemp->next;
pCloneNode = pCloneNode->next;
pTemp->next = pCloneNode->next;
pTemp = pTemp->next;
}
return pCloneHead;
}
//封装复制链表的操作
pNode Clone(pNode pHead)
{
if(pHead == NULL)
{
return NULL;
}
CloneNodes(pHead);
ConnectSiblingNodes(pHead);
return ReconnectNodes(pHead);
}
//-----------------------------------
//创建一个单链表
void createLinkedList(pNode *pHead,int num)
{
if(num <= 0)
{
return;
}
int data;
*pHead = (pNode)malloc(sizeof(Node));
if(!pHead)
{
exit(-1);
}
cin>>data;
(*pHead)->data = data;
(*pHead)->next = NULL;
(*pHead)->sibling = NULL;
num--;
pNode pTemp = *pHead;
while(num > 0)
{
cin>>data;
pNode pNew = (pNode)malloc(sizeof(Node));
if(!pNew)
{
exit(-1);
}
pNew->data = data;
pNew->next = NULL;
pNew->sibling = NULL;
pTemp->next = pNew;
pTemp = pNew;
num--;
}
}
//创建一个复杂链表,为测试方便,这里定死了
void createComplexList(pNode *pHead)
{
//先创建一个单链表
createLinkedList(pHead,5);
//然后为每个结点设置sibling指针
(*pHead)->sibling = (*pHead)->next->next;
(*pHead)->next->sibling = (*pHead)->next->next->next->next;
pNode pTemp = (*pHead)->next->next->next;
pTemp->sibling = (*pHead)->next;
}
//打印链表
void display(pNode pHead)
{
pNode pTemp = pHead;
while(pTemp != NULL)
{
cout<<pTemp->data<<" ";
pTemp = pTemp->next;
}
cout<<endl;
}
//销毁链表
void Destroy(pNode pHead)
{
if(pHead == NULL)
{
return;
}
pNode p = pHead,q;
while(p != NULL)
{
q = p->next;
free(p);
p = q;
}
}
int main()
{
pNode pHead = NULL;
createComplexList(&pHead);
cout<<"原始链表如下:"<<endl;
display(pHead);
pNode pCloneHead = Clone(pHead);
if(pCloneHead != NULL)
{
cout<<"克隆链表如下"<<endl;
display(pCloneHead);
cout<<"打印克隆链表每个结点的值以及sibling结点的值如下\n";
pNode p = pCloneHead;
while(p != NULL)
{
cout<<p->data<<" ";
if(p->sibling != NULL)
{
cout<<p->sibling->data<<endl;
}
else
{
cout<<"NULL\n";
}
p = p->next;
}
}
Destroy(pHead);
return 0;
}