#include<iostream>
using namespace std;
typedef struct ComplexNode
{
int _data;
struct ComplexNode* _next;
struct ComplexNode* _random;
}ComplexNode;
using namespace std;
typedef struct ComplexNode
{
int _data;
struct ComplexNode* _next;
struct ComplexNode* _random;
}ComplexNode;
ComplexNode* BuyNode(int data)
{
ComplexNode* node = (ComplexNode*)malloc(sizeof(ComplexNode));
node->_data = data;
node->_next = NULL;
node->_random = NULL;
return node;
}
ComplexNode* CopyList(ComplexNode* head)
{
//插入拷贝节点
ComplexNode* cur = head;
while (cur)
{
ComplexNode* next = cur->_next;
ComplexNode* copy = BuyNode(cur->_data);
cur->_next = copy;
copy->_next = next;
cur = next;
}
// 置random
{
ComplexNode* node = (ComplexNode*)malloc(sizeof(ComplexNode));
node->_data = data;
node->_next = NULL;
node->_random = NULL;
return node;
}
ComplexNode* CopyList(ComplexNode* head)
{
//插入拷贝节点
ComplexNode* cur = head;
while (cur)
{
ComplexNode* next = cur->_next;
ComplexNode* copy = BuyNode(cur->_data);
cur->_next = copy;
copy->_next = next;
cur = next;
}
// 置random
cur = head;
while (cur)
{
ComplexNode* copy = cur->_next;
if (cur->_random)
copy->_random = cur->_random->_next;
cur = copy->_next;
}
// 拆解
ComplexNode* copyHead = NULL;
ComplexNode* copyTail = NULL;
cur = head;
while (cur)
{
ComplexNode* copy = cur->_next;
ComplexNode* next = copy->_next;
if (copyHead == NULL)
{
copyHead = copyTail = copy;
}
else
{
copyTail->_next = copy;
copyTail = copy;
}
cur = next;
}
return copyHead;
}
void PrintComplexList(ComplexNode* list)
{
ComplexNode* cur = list;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_next;
}
cout << "NULL" << endl;
cur = list;
while (cur)
{
if (cur->_random)
cout << cur->_random->_data << " ";
else
cout << "NULL" << endl;
cur = cur->_next;
}
cout << endl;
}
int main()
{
ComplexNode* n1 = BuyNode(1);
ComplexNode* n2 = BuyNode(2);
ComplexNode* n3 = BuyNode(3);
ComplexNode* n4 = BuyNode(4);
while (cur)
{
ComplexNode* copy = cur->_next;
if (cur->_random)
copy->_random = cur->_random->_next;
cur = copy->_next;
}
// 拆解
ComplexNode* copyHead = NULL;
ComplexNode* copyTail = NULL;
cur = head;
while (cur)
{
ComplexNode* copy = cur->_next;
ComplexNode* next = copy->_next;
if (copyHead == NULL)
{
copyHead = copyTail = copy;
}
else
{
copyTail->_next = copy;
copyTail = copy;
}
cur = next;
}
return copyHead;
}
void PrintComplexList(ComplexNode* list)
{
ComplexNode* cur = list;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_next;
}
cout << "NULL" << endl;
cur = list;
while (cur)
{
if (cur->_random)
cout << cur->_random->_data << " ";
else
cout << "NULL" << endl;
cur = cur->_next;
}
cout << endl;
}
int main()
{
ComplexNode* n1 = BuyNode(1);
ComplexNode* n2 = BuyNode(2);
ComplexNode* n3 = BuyNode(3);
ComplexNode* n4 = BuyNode(4);
n1->_next = n2;
n2->_next = n3;
n3->_next = n4;
n4->_next = NULL;
n2->_next = n3;
n3->_next = n4;
n4->_next = NULL;
n1->_random = n4;
n2->_random = n2;
n3->_random = n1;
n4->_random = NULL;
n2->_random = n2;
n3->_random = n1;
n4->_random = NULL;
PrintComplexList(n1);
ComplexNode* copy = CopyList(n1);
PrintComplexList(copy);
ComplexNode* copy = CopyList(n1);
PrintComplexList(copy);
return 0;
}
}
1303

被折叠的 条评论
为什么被折叠?



