复杂链表的赋值
#include<iostream>
#include<stdio.h>
using namespace std;
struct ComplexNode{?
int ?_data ; // 數(shù)據(jù)?
struct ComplexNode * _next; // 指向下一個節(jié)點的指針?
struct ComplexNode * _random; // 指向隨機節(jié)點(可以是鏈表中的任意節(jié)點 or 空)?
};
ComplexNode* CreatComplexNode(int data)
{
ComplexNode* ?Node=(ComplexNode*)malloc(sizeof(ComplexNode));
if(Node==NULL)
{
printf("out of memeory\n!!");
return NULL;
}
Node->_data=data;
Node->_next=NULL;
Node->_random=NULL;
return Node;
}
ComplexNode* CreatComplexList()
{
ComplexNode* ?n1=CreatComplexNode(1);
ComplexNode* ?n2=CreatComplexNode(2);
ComplexNode* ?n3=CreatComplexNode(3);
ComplexNode* ?n4=CreatComplexNode(4);
n1->_next=n2;
n2->_next=n3;
n3->_next=n4;
n4->_next=NULL;
n1->_random=n3;
n2->_random=NULL;
n3->_random=n2;
n4->_random=n4;
return n1;
}
ComplexNode* ?CopyComplexNode(ComplexNode* pHead)
{
ComplexNode* newnode=NULL;
ComplexNode* cur=pHead;
while(cur)//給新節(jié)點賦值
{
newnode= CreatComplexNode(cur->_data);
newnode->_next=cur->_next;
cur->_next=newnode;
cur=newnode->_next;
}
//給新節(jié)點的random賦值
cur=pHead;
ComplexNode* Nextnode=NULL;
while(cur)
{
Nextnode=cur->_next;
Nextnode->_random=cur->_random;
cur=Nextnode->_next;
}
//拆分
newnode=pHead->_next;
Nextnode=newnode;
cur=Nextnode->_next;
pHead->_next=cur;
while(cur)
{
newnode=cur->_next;
newnode=newnode->_next;
cur->_next=newnode->_next;
cur=cur->_next;
}
return newnode;
}
void print(ComplexNode* pHead)
{
if(pHead==NULL)
return ;
ComplexNode* p=pHead;
while(p)
?{
if(p->_random)
{
printf("%d:random->%d \n",p->_data,p->_random->_data);
}
else
{
printf("%d:random->NULL\n",p->_data);
}
?p= p->_next;
?} ?
}
int main()
{
ComplexNode pNode;
ComplexNode* ll=CreatComplexList( );
print(ll);
ComplexNode* l2=CopyComplexNode(ll);
print(l2);
system("pause");
return 0;
} 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
#include<stdio.h>
using namespace std;
struct ComplexNode{?
int ?_data ; // 數(shù)據(jù)?
struct ComplexNode * _next; // 指向下一個節(jié)點的指針?
struct ComplexNode * _random; // 指向隨機節(jié)點(可以是鏈表中的任意節(jié)點 or 空)?
};
ComplexNode* CreatComplexNode(int data)
{
ComplexNode* ?Node=(ComplexNode*)malloc(sizeof(ComplexNode));
if(Node==NULL)
{
printf("out of memeory\n!!");
return NULL;
}
Node->_data=data;
Node->_next=NULL;
Node->_random=NULL;
return Node;
}
ComplexNode* CreatComplexList()
{
ComplexNode* ?n1=CreatComplexNode(1);
ComplexNode* ?n2=CreatComplexNode(2);
ComplexNode* ?n3=CreatComplexNode(3);
ComplexNode* ?n4=CreatComplexNode(4);
n1->_next=n2;
n2->_next=n3;
n3->_next=n4;
n4->_next=NULL;
n1->_random=n3;
n2->_random=NULL;
n3->_random=n2;
n4->_random=n4;
return n1;
}
ComplexNode* ?CopyComplexNode(ComplexNode* pHead)
{
ComplexNode* newnode=NULL;
ComplexNode* cur=pHead;
while(cur)//給新節(jié)點賦值
{
newnode= CreatComplexNode(cur->_data);
newnode->_next=cur->_next;
cur->_next=newnode;
cur=newnode->_next;
}
//給新節(jié)點的random賦值
cur=pHead;
ComplexNode* Nextnode=NULL;
while(cur)
{
Nextnode=cur->_next;
Nextnode->_random=cur->_random;
cur=Nextnode->_next;
}
//拆分
newnode=pHead->_next;
Nextnode=newnode;
cur=Nextnode->_next;
pHead->_next=cur;
while(cur)
{
newnode=cur->_next;
newnode=newnode->_next;
cur->_next=newnode->_next;
cur=cur->_next;
}
return newnode;
}
void print(ComplexNode* pHead)
{
if(pHead==NULL)
return ;
ComplexNode* p=pHead;
while(p)
?{
if(p->_random)
{
printf("%d:random->%d \n",p->_data,p->_random->_data);
}
else
{
printf("%d:random->NULL\n",p->_data);
}
?p= p->_next;
?} ?
}
int main()
{
ComplexNode pNode;
ComplexNode* ll=CreatComplexList( );
print(ll);
ComplexNode* l2=CopyComplexNode(ll);
print(l2);
system("pause");
return 0;
} 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結