复杂链表的复制
2016-04-16 11:48
246 查看
对于链表的复制见的也比较多了,但对于复杂链表的复制,主要存在的问题是复杂链表中节点存在random指针,但它指向的方向是任意的,因此在对复杂链表进行复制的过程中,对于确定random指针的指向还存在着很大的问题。
解决这个问题的一个方法是,将原来链表的每个节点进行复制,而复制的节点就插入到原来节点的后面,这样复制节点的random指针指向,就是原来节点random指针指向的_next节点。代码实现如下:
typedef int DataType;
typedef struct ComplexNode
{
DataType _data;
struct ComplexNode *_next;
struct ComplexNode *_random;
ComplexNode(DataType data)
:_data(data)
, _next(NULL)
, _random(NULL)
{}
}ComplexNode;
ComplexNode *copy(ComplexNode *head)
{
//复制节点
ComplexNode *cur = head;
while (cur)
{
ComplexNode *tem = new ComplexNode(cur->_data);
tem->_next = cur->_next;
cur->_next = tem;
cur = tem->_next;
}
//找random
cur = head;
ComplexNode *tem;
while (cur)
{
tem = cur->_next;
if (cur->_random)
tem->_random = cur->_random->_next;
cur = tem->_next;
}
//分离
cur = head;
ComplexNode *newhead=NULL, *newtail=NULL;
if (cur)
{
newhead = newtail = cur->_next;
cur->_next = newhead->_next;
cur = cur->_next;
}
while (cur)
{
newtail->_next = cur->_next;
cur->_next = cur->_next->_next;
newtail = newtail->_next;
cur = cur->_next;
}
return newhead;
}
ComplexNode *Create()
{
ComplexNode *head1 = new ComplexNode(1);
ComplexNode *head2 = new ComplexNode(2);
ComplexNode *head3 = new ComplexNode(3);
ComplexNode *head4 = new ComplexNode(4);
head1->_random = head3;
head2->_random = head4;
head3->_random = head2;
head4->_random = NULL;
head1->_next = head2;
head2->_next = head3;
head3->_next = head4;
head4->_next = NULL;
return head1;
}
void print(ComplexNode *head)
{
while (head)
{
cout << head->_data << ":";
if (head->_random)
cout << head->_random->_data << " ";
else
cout << "NULL" << " ";
head = head->_next;
}
}
int main()
{
ComplexNode *ret = Create();
print(ret);
getchar();
return 0;
}
解决这个问题的一个方法是,将原来链表的每个节点进行复制,而复制的节点就插入到原来节点的后面,这样复制节点的random指针指向,就是原来节点random指针指向的_next节点。代码实现如下:
typedef int DataType;
typedef struct ComplexNode
{
DataType _data;
struct ComplexNode *_next;
struct ComplexNode *_random;
ComplexNode(DataType data)
:_data(data)
, _next(NULL)
, _random(NULL)
{}
}ComplexNode;
ComplexNode *copy(ComplexNode *head)
{
//复制节点
ComplexNode *cur = head;
while (cur)
{
ComplexNode *tem = new ComplexNode(cur->_data);
tem->_next = cur->_next;
cur->_next = tem;
cur = tem->_next;
}
//找random
cur = head;
ComplexNode *tem;
while (cur)
{
tem = cur->_next;
if (cur->_random)
tem->_random = cur->_random->_next;
cur = tem->_next;
}
//分离
cur = head;
ComplexNode *newhead=NULL, *newtail=NULL;
if (cur)
{
newhead = newtail = cur->_next;
cur->_next = newhead->_next;
cur = cur->_next;
}
while (cur)
{
newtail->_next = cur->_next;
cur->_next = cur->_next->_next;
newtail = newtail->_next;
cur = cur->_next;
}
return newhead;
}
ComplexNode *Create()
{
ComplexNode *head1 = new ComplexNode(1);
ComplexNode *head2 = new ComplexNode(2);
ComplexNode *head3 = new ComplexNode(3);
ComplexNode *head4 = new ComplexNode(4);
head1->_random = head3;
head2->_random = head4;
head3->_random = head2;
head4->_random = NULL;
head1->_next = head2;
head2->_next = head3;
head3->_next = head4;
head4->_next = NULL;
return head1;
}
void print(ComplexNode *head)
{
while (head)
{
cout << head->_data << ":";
if (head->_random)
cout << head->_random->_data << " ";
else
cout << "NULL" << " ";
head = head->_next;
}
}
int main()
{
ComplexNode *ret = Create();
print(ret);
getchar();
return 0;
}
相关文章推荐
- 如何在 Linux 上复制文件/文件夹到远程系统?
- sql server 复制表从一个数据库到另一个数据库
- C#中数组初始化与数组元素复制的方法
- 浅析C#的复制和克隆
- C#实现复制文件夹中文件到另一个文件夹的方法
- C#实现把dgv里的数据完整的复制到一张内存表的方法
- SQL Server数据复制到的Access两步走
- 利用xcopy命令实现本地文件复制到远程服务器的方法
- php检索或者复制远程文件的方法
- php实现复制移动文件的方法
- php不使用copy()函数复制文件的方法
- 复制Input内容的js代码_支持所有浏览器,修正了Firefox3.5以上的问题
- Javascript 实现复制(Copy)动作方法大全
- JavaScript深度复制(deep clone)的实现方法
- Zero Clipboard实现浏览器复制到剪贴板的方法(多个复制按钮)
- jQuery zclip插件实现跨浏览器复制功能
- 复制本贴标题和地址的js代码
- Sybase 复制与热切换数据
- JavaScript实现复制功能各浏览器支持情况实测
- c#文件的复制,移动,创建(实例代码)