复杂链表的复制


复杂链表的复制


程序代码如下:


Complex.h


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
	DataType data;
	struct Node* next;
	struct Node* rand;
}Node, *pNode, List, *pList;
// 复杂链表的复制 
pNode CopyComplex(pNode pHead);


Complex.c

#include "Linklist Interface.h"

//复杂链表的复制
pNode CopyComplex(pNode pHead)
{
	//assert(pHead);
	pNode cur1 = pHead;
	pNode cur2 = NULL;
	pNode CopyNode = NULL;

	if (NULL == pHead)
		return NULL;
	// copy node and link to every node 复制节点并连接到每个节点
	while (cur1)
	{
		cur2 = cur1->next;
		cur1->next = BuyNode(cur1->data);
		cur1->next->next = cur2;
		cur1 = cur2;
	}
	cur1 = pHead;
	// set copy node rand  设置复制节点的rand随机指针域
	while (cur1)
	{
		CopyNode = cur1->next;
		CopyNode->rand = (cur1->rand != NULL) ? cur1->rand->next : NULL;
		cur1 = CopyNode->next;
	}
	cur1 = pHead;
	pNode ret = cur1->next;
	// split 分离链表,返回复制成功后的新链表
	while (cur1)
	{
		cur2 = cur1->next->next;
		CopyNode = cur1->next;
		cur1->next = cur2;
		CopyNode->next = (cur2 != NULL) ? cur2->next : NULL;
		cur1 = cur2;
	}
	return ret;
}

void PrintLinkList(pList plist)
{
	pNode tmp = plist;
	while (tmp)
	{
		//复杂链表打印
		printf("%d::%d -->", tmp->data, tmp->rand->data);
		tmp = tmp->next;
	}
	printf("NULL\n");
}

test.c

#include "Linklist Interface.h"

Test()
{
	pList plist, cplist;
	pNode newNode1, newNode2, newNode3, newNode4, newNode5;
	plist = BuyNode(0);
	newNode1 = BuyNode(1);
	newNode2 = BuyNode(2);
	newNode3 = BuyNode(3);
	newNode4 = BuyNode(4);
	newNode5 = BuyNode(5);

	plist->next = newNode1;
	newNode1->next = newNode2;
	newNode2->next = newNode3;
	newNode3->next = newNode4;
	newNode4->next = newNode5;
	newNode5->next = NULL;

	plist->rand = newNode4;
	newNode1->rand = newNode3;
	newNode2->rand = plist;
	newNode3->rand = newNode5;
	newNode4->rand = newNode1;
	newNode5->rand = newNode2;

	PrintLinkList(plist);
	cplist = CopyComplex(plist);
	PrintLinkList(cplist);
}

int main()
{
	Test();
	system("pause");
	return 0;

}

程序运行结果如下:


在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity中,如果你想要复制一个复杂的数据结构,如链表(通常在脚本中用自定义的Node类实现),你需要逐个节点地复制,并确保子节点和指针也被正确复制。这里是一个简化的步骤: 1. **定义节点类** (如果尚未存在): ```csharp public class Node<T> { public T data; public Node<T> next; // 其他可能的属性或方法... } ``` 2. **复制链表头结点**: 创建一个新的`Node`实例,将原始头结点的数据复制过去,然后设置新的头结点的`next`为复制后的`Node`。 ```csharp Node<T> newNode = new Node<T>(); newNode.data = originalHead.data; newNode.next = null; // 初始化新节点的next为null ``` 3. **递归复制链表**: 使用递归函数来遍历原链表,对于每个节点,复制其数据并更新新链表中的下一个节点指向新复制的节点。 ```csharp public Node<T> CloneNode(Node<T> node, Node<T> clonedNode) { if (node != null) { clonedNode.data = node.data; clonedNode.next = CloneNode(node.next, clonedNode.next); // 递归处理下一个节点 } return clonedNode; } Node<T> clonedHead = CloneNode(originalHead, newNode); ``` 4. **处理复杂节点**: 如果链表中的节点包含更复杂的对象,比如数组、列表等,你也需要递归地复制这些内容。 5. **检查和连接**: 完成复制后,确保新链表的最后一个节点的`next`指向原链表的最后一个节点,以保证连接完整。 ```csharp if (clonedHead.next == null) // 如果新链表不为空,连接最后两个节点 { clonedHead.next = originalHead.next; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值