剑指Offer-26-复杂链表的复制

93人阅读 评论(0)

struct ComplexNode
{
int m_nValue;
ComplexNode* m_pNext;
ComplexNode* m_pSibling;
};

#include <iostream>
using namespace std;

struct ComplexListNode {
int m_nValue;
ComplexListNode* m_pNext;
ComplexListNode* m_pSibling;
};

/**
* 使用一般方式进行复制，时间复杂度o(n*n)
*/
return NULL;

//第一步：复制原始链表的每一个结点，按照p->m_pNext连接起来
while(p) {
ComplexListNode *newP = new ComplexListNode();
newP->m_nValue = p->m_nValue;
newP->m_pNext = NULL;
newP->m_pSibling = NULL;

preNewP->m_pNext = newP;
preNewP = newP;

p=p->m_pNext;
}

//第二步：设置每个结点的m_pSibling指针
//如果原始链表的头结点开始沿着m_pNext经过s步找到结点S,那么复制链表上结点N的N'的m_pSibling（S'）
//离复制链表的头结点的距离也是沿着m_pNext指针s步。
while(p) {
if(p->m_pSibling != NULL) {
while(sibling != p->m_pSibling) {
sibling = sibling->m_pNext;
newSibling = newSibling->m_pNext;
}
newp->m_pSibling = newSibling;
}
p = p->m_pNext;
newp = newp->m_pNext;
}
}

ComplexListNode* CreateNode(int nValue)
{
ComplexListNode* pNode = new ComplexListNode();

pNode->m_nValue = nValue;
pNode->m_pNext = NULL;
pNode->m_pSibling = NULL;

return pNode;
}

void BuildNodes(ComplexListNode* pNode, ComplexListNode* pNext, ComplexListNode* pSibling)
{
if(pNode != NULL)
{
pNode->m_pNext = pNext;
pNode->m_pSibling = pSibling;
}
}

{
while(pNode != NULL)
{
cout<<"The value of this node is:"<<pNode->m_nValue<<".";

if(pNode->m_pSibling != NULL)
cout<<"The value of its sibling is:"<<pNode->m_pSibling->m_nValue<<".";
else
cout<<"This node does not have a sibling.";

cout<<endl;

pNode = pNode->m_pNext;
}
}

// ====================测试代码====================
{
if(testName != NULL)
cout<<testName;

cout<<"The original list is:"<<endl;

cout<<"The cloned list is:"<<endl;
}

//          -----------------
//         \|/              |
//  1-------2-------3-------4-------5
//  |       |      /|\             /|\
//  --------+--------               |
//          -------------------------
void Test1()
{
ComplexListNode* pNode1 = CreateNode(1);
ComplexListNode* pNode2 = CreateNode(2);
ComplexListNode* pNode3 = CreateNode(3);
ComplexListNode* pNode4 = CreateNode(4);
ComplexListNode* pNode5 = CreateNode(5);

BuildNodes(pNode1, pNode2, pNode3);
BuildNodes(pNode2, pNode3, pNode5);
BuildNodes(pNode3, pNode4, NULL);
BuildNodes(pNode4, pNode5, pNode2);

Test("Test1", pNode1);
}

// m_pSibling指向结点自身
//          -----------------
//         \|/              |
//  1-------2-------3-------4-------5
//         |       | /|\           /|\
//         |       | --             |
//         |------------------------|
void Test2()
{
ComplexListNode* pNode1 = CreateNode(1);
ComplexListNode* pNode2 = CreateNode(2);
ComplexListNode* pNode3 = CreateNode(3);
ComplexListNode* pNode4 = CreateNode(4);
ComplexListNode* pNode5 = CreateNode(5);

BuildNodes(pNode1, pNode2, NULL);
BuildNodes(pNode2, pNode3, pNode5);
BuildNodes(pNode3, pNode4, pNode3);
BuildNodes(pNode4, pNode5, pNode2);

Test("Test2", pNode1);
}

// m_pSibling形成环
//          -----------------
//         \|/              |
//  1-------2-------3-------4-------5
//          |              /|\
//          |               |
//          |---------------|
void Test3()
{
ComplexListNode* pNode1 = CreateNode(1);
ComplexListNode* pNode2 = CreateNode(2);
ComplexListNode* pNode3 = CreateNode(3);
ComplexListNode* pNode4 = CreateNode(4);
ComplexListNode* pNode5 = CreateNode(5);

BuildNodes(pNode1, pNode2, NULL);
BuildNodes(pNode2, pNode3, pNode4);
BuildNodes(pNode3, pNode4, NULL);
BuildNodes(pNode4, pNode5, pNode2);

Test("Test3", pNode1);
}

// 只有一个结点
void Test4()
{
ComplexListNode* pNode1 = CreateNode(1);
BuildNodes(pNode1, NULL, pNode1);

Test("Test4", pNode1);
}

// 鲁棒性测试
void Test5()
{
Test("Test5", NULL);
}

int main() {
Test1();
Test2();
Test3();
Test4();
Test5();
return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：34950次
• 积分：1242
• 等级：
• 排名：千里之外
• 原创：83篇
• 转载：51篇
• 译文：0篇
• 评论：1条