复杂链表的构造与拷贝
来源:互联网 发布:西安79家网络诈骗公司 编辑:程序博客网 时间:2024/05/16 14:59
Q:复杂链表节点定义:
struct ComplexLinkListNode{int value;ComplexLinkListNode *next;ComplexLinkListNode *random;};
next 指向链表中的下一个节点,random指向链表中的任一节点或者为NULL,完成复杂链表的构造和拷贝;
A:
定义数组data为链表依次节点的整数值集合;定义数组randomIndex为链表依次节点的random指针所指节点,其中1代表头结点,依次类推。
/*复杂链表的构造*/
//首先根据data构造普通单链表,random指针置空
ComplexLinkListNode* ComplexLinkListBuild(int data[],int length){<span style="white-space:pre"></span>if(data == NULL){cout<<"Empty Database!"<<endl;return NULL;}<span style="white-space:pre"></span>ComplexLinkListNode *curr = new ComplexLinkListNode();<span style="white-space:pre"></span>ComplexLinkListNode *head = curr;<span style="white-space:pre"></span>curr->value = data[0];<span style="white-space:pre"></span>curr->next = NULL;<span style="white-space:pre"></span>for(int i = 1;i<=length-1;++i)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>ComplexLinkListNode *temp = new ComplexLinkListNode();<span style="white-space:pre"></span>temp->value = data[i];<span style="white-space:pre"></span>temp->next = NULL;<span style="white-space:pre"></span>curr->next =temp;<span style="white-space:pre"></span>curr = curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return head;}
//根据数组<span style="font-family: Arial, Helvetica, sans-serif;">randomIndex重构random指针</span>void ComplexLinkListRandomConstruct(ComplexLinkListNode *head,int randomIndex[],int length){<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>ComplexLinkListNode *randomTemp = head;<span style="white-space:pre"></span>for(int i = 0;i<=length -1;i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(randomIndex[i] == 0){curr->random = NULL;}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>for(int j = 1;j<randomIndex[i];j++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>randomTemp = randomTemp->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>curr->random = randomTemp;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>randomTemp = head;<span style="white-space:pre"></span>curr =curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}
<span style="font-family: Arial, Helvetica, sans-serif;">/*复杂链表的拷贝*/</span>
//拷贝每一个复杂链表节点,并将next指向新拷贝的节点
void ComplexLinkListClone(ComplexLinkListNode *head){<span style="white-space:pre"></span>if(head == NULL)cout<<"Empty Link List!"<<endl;<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>while(curr != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>ComplexLinkListNode *nodeCopy = new ComplexLinkListNode();<span style="white-space:pre"></span>nodeCopy->value = curr->value;<span style="white-space:pre"></span>nodeCopy->next = curr->next;<span style="white-space:pre"></span>nodeCopy->random = NULL;<span style="white-space:pre"></span>curr->next = nodeCopy;<span style="white-space:pre"></span>curr = nodeCopy->next;<span style="white-space:pre"></span>}}//拷贝链表节点的<span style="font-family: Arial, Helvetica, sans-serif;">Random指针</span>void ComplexLinkListRandom(ComplexLinkListNode *head){<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>ComplexLinkListNode *currCopy;<span style="white-space:pre"></span>while(curr)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>currCopy = curr->next;<span style="white-space:pre"></span>if(curr->random){currCopy->random = curr->random->next;}<span style="white-space:pre"></span>curr = currCopy->next;<span style="white-space:pre"></span>}}//将原始复杂链表和拷贝复杂链表分离ComplexLinkListNode* ComplexLinkListSplit(ComplexLinkListNode *head){<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>ComplexLinkListNode *headCopy = head->next;<span style="white-space:pre"></span>ComplexLinkListNode *currCopy = head->next;<span style="white-space:pre"></span>while(curr && currCopy->next)<span style="white-space:pre"></span>{<span style="white-space:pre"></span><span style="white-space:pre"></span>curr->next = currCopy->next;<span style="white-space:pre"></span>currCopy->next = currCopy->next->next;<span style="white-space:pre"></span>curr =curr->next;<span style="white-space:pre"></span>if(curr)currCopy = curr->next;<span style="white-space:pre"></span><span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>curr->next = NULL;<span style="white-space:pre"></span>return headCopy;}
/*复杂链表的遍历*/
//依next域依次遍历复杂链表
void ComplexLinkListDisplay(ComplexLinkListNode *head){<span style="white-space:pre"></span>if(head == NULL)cout<<"Empty Link List!"<<endl;<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>while(curr != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cout<<curr->value<<" ";<span style="white-space:pre"></span>curr = curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>cout<<endl;}
//将复杂链表中节点random指针所指结点依次遍历void ComplexLinkListDisplayByRandom(ComplexLinkListNode *head){<span style="white-space:pre"></span>if(head == NULL)cout<<"Empty Link List!"<<endl;<span style="white-space:pre"></span>ComplexLinkListNode *curr = head;<span style="white-space:pre"></span>while(curr != NULL)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(curr->random == NULL){cout<<"NULL"<<" ";}<span style="white-space:pre"></span>else{cout<<curr->random->value<<" ";}<span style="white-space:pre"></span>curr = curr->next;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>cout<<endl;}
/*测试用例*/
void ComplexLinkListCopyTest(){<span style="white-space:pre"></span><span style="white-space:pre"></span>int data[] = {1,2,3,4,5,6,7};<span style="white-space:pre"></span>int randomIndex[] = {2,1,0,0,7,5,6};<span style="white-space:pre"></span>int length = sizeof(data)/sizeof(int);<span style="white-space:pre"></span>ComplexLinkListNode *head = ComplexLinkListBuild(data,length);<span style="white-space:pre"></span>//Complex Link List Constructor<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>ComplexLinkListRandomConstruct(head,randomIndex,length);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>//Complex Link List Copy<span style="white-space:pre"></span>ComplexLinkListClone(head);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>ComplexLinkListRandom(head);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);<span style="white-space:pre"></span>ComplexLinkListNode *headCopy = ComplexLinkListSplit(head);<span style="white-space:pre"></span>ComplexLinkListDisplay(headCopy);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(headCopy);<span style="white-space:pre"></span>ComplexLinkListDisplay(head);<span style="white-space:pre"></span>ComplexLinkListDisplayByRandom(head);}
0 0
- 复杂链表的构造与拷贝
- 链表:复杂链表的拷贝
- 复杂链表的拷贝实现
- 拷贝与拷贝构造的优化
- 复杂链表类的拷贝构造函数的实现(1)
- 写一个类,实现复杂对象的拷贝构造
- 深度拷贝复杂链表的巧妙解法
- 剑指offer——复杂链表的拷贝
- c++中 拷贝构造函数的深拷贝和浅拷贝--“浅拷贝”与“深拷贝”
- c++中类的位拷贝与拷贝构造函数(浅拷贝与深拷贝)
- 拷贝构造与深拷贝
- 拷贝构造与深拷贝
- 拷贝构造与拷贝赋值
- 模板拷贝构造与赋值的不对称
- c++的堆与拷贝构造函数
- 复杂链表拷贝(面试题)
- 练习26 复杂链表拷贝
- 堆和拷贝构造函数--深拷贝与浅拷贝,用于类型转换的构造函数
- Android 笔记 Listview中加入Button的冲突解决
- Android Service完全解析,关于服务你所需知道的一切(下)
- java hashcode equals
- 关于js监控onbeforeunload与onunload事件
- 异步下载图片的任务
- 复杂链表的构造与拷贝
- 【多线程】秒杀多线程面试题目
- mysql性能优化-慢查询分析、优化索引和配置
- 恢复控制文件故障详细步骤
- Ubuntu NTFS文件系统下没有执行权限的问题
- 处理百万级以上的数据处理
- flume+kafka+storm+redis/mysql启动命令记录
- 快速排序
- PB中扩展嵌套结构