此次程序是链表操作中的几个函数的实现,包括(判断俩链表的相交、求俩链表的交集、拷贝复杂链表)
1、判断俩个链表是否相交,但是链表可能带环
程序代码:
int HasCrossWithCycle(LinkNode* head1,LinkNode* head2){
if(head1->next==NULL||head2->next==NULL){return 0;
}
LinkNode* ret1=HasCycle(head1);
LinkNode* ret2= HasCycle(head2);
LinkNode* cur1 = NULL;
LinkNode* cur2 = NULL;
if(ret1==NULL && ret2==NULL){
LinkNode* cur1 = head1->next;
LinkNode*cur2 = head2->next;
for(;cur1!=NULL;cur1 = cur1->next){
for(cur2 = ret2;cur2!=NULL;cur2= cur2->next){
if(cur1 == cur2){
return 1;
}
}
}
return 0;
}
if((ret1==NULL&&ret2!=NULL)||(ret1!=NULL&&ret2==NULL)){
return 0;
}
if(ret1!=NULL&&ret2!=NULL){
if(ret1==ret2){
return 1;
}
while(ret1->next!=ret1){
if(ret1==ret2){
return 1;
}
ret1 = ret1->next;
}
return 0;
}
}
检测代码:
//判断俩个链表是否相交(链表可能带环)的检测函数
textHasCrossWithCycle(){
LinkNode* head1 = (LinkNode *)malloc(sizeof(LinkNode));
LinkListPushFront(&head1,CreateNewNode('a'));
LinkListPushFront(&head1,CreateNewNode('b'));
LinkListPushFront(&head1,CreateNewNode('c'));
LinkListPushFront(&head1,CreateNewNode('d'));
head1->next->next->next->next->next = head1->next->next->next;
LinkNode* head2 = (LinkNode *)malloc(sizeof(LinkNode));
LinkListPushFront(&head2,CreateNewNode('c'));
LinkListPushFront(&head2,CreateNewNode('d'));
head2->next->next->next = head1->next->next->next->next;
printf("\n*********Has cross with cycle********\n");
int ret=HasCrossWithCycle(head1,head2);
printf("expect:1 actual:%d\n",ret);
}
调试结果:
2、求俩个链表的交集
程序代码:
//求俩个有序链表的交集
//返回表示交集的新链表
LinkNode* UnionSet(LinkNode* head1,LinkNode* head2){
LinkNode* cur1 = head1->next;//定义指向链表1的指针
LinkNode* cur2 = head2->next;//定义指向链表2的zhizhen
LinkNode* new_head = (LinkNode*)malloc(sizeof(LinkNode));
LinkNode* cur = new_head;
while(cur->next!=NULL){
cur = cur->next;
}
LinkNode* tail = cur;//遍历俩个链表进行对链表的结点内容比较,将相同的结点插入到新链表中,在每次往新链表内插入结点时,将结点的内容与新链表的内容进行比较(存在即不插入,反之插入)
for(;cur1!=NULL;cur1 = cur1->next){
for(cur2=head2->next;cur2!=NULL;cur2= cur2->next){
if(cur1->data == cur2->data){
if(new_head->next != NULL){
cur = new_head->next;
for(cur = new_head->next;cur!=NULL;cur= cur->next){
if(cur1->data == cur->data){
break;
}
}
tail->next = CreateNewNode(cur1->data);
tail = tail->next;
break;
}
tail->next = CreateNewNode(cur1->data);
tail = tail->next;
}
}
}
return new_head;
}
检测代码:
//求交集函数的检测函数
textUnionSet(){
LinkNode* head1 = (LinkNode *)malloc(sizeof(LinkNode));
LinkListPushFront(&head1,CreateNewNode('a'));
LinkListPushFront(&head1,CreateNewNode('b'));
LinkListPushFront(&head1,CreateNewNode('c'));
LinkListPushFront(&head1,CreateNewNode('d'));
LinkNode* head2 = (LinkNode *)malloc(sizeof(LinkNode));
LinkListPushFront(&head2,CreateNewNode('a'));
LinkListPushFront(&head2,CreateNewNode('b'));
LinkListPushFront(&head2,CreateNewNode('n'));
LinkListPushFront(&head2,CreateNewNode('m'));
LinkNode* ret = UnionSet(head1,head2);
printfList(ret);
}
检测结果:
3、拷贝复杂链表
程序代码:
//复杂链表的拷贝
//此处的复杂链表有俩个指向(next、randon)
typedef struct ComplexNode{
LinkType data;
struct ComplexNode* next;
struct ComplexNode* random;
}ComplexNode;
ComplexNode* CreateNewNode1(LinkType value){
ComplexNode* new_Node = (ComplexNode*)malloc(sizeof(ComplexNode));
new_Node->data = value;
new_Node->next = NULL;
new_Node->random = NULL;
return new_Node;
}
//尾插,
void LinkListPushBack(ComplexNode**head,ComplexNode* ret){
if(head==NULL){
return;
}
//ComplexNode* tail = NULL;
ComplexNode* cur = (*head);
//if(cur->next == NULL){
// ComplexNode* tail = cur;
//}
while(cur->next != NULL){
cur = cur->next;
}
ComplexNode* tail = cur;
tail->next = ret;
tail= tail->next;
}
ComplexNode* CopyComplex(ComplexNode* head){
if(head->next==NULL){
return NULL;
}
ComplexNode* new_head = (ComplexNode*)malloc(sizeof(ComplexNode));
ComplexNode* cur_new = new_head;
ComplexNode* cur = head->next;
while(cur!=NULL){
LinkListPushBack(&new_head,CreateNewNode1(cur->data);
cur = cur->next;
}
cur_new = new_head->next;
cur = head->next;
while(cur != NULL){
ComplexNode * cur1 = head->next;
size_t count= 0;
for(cur1= head->next;cur1!=NULL;cur1= cur1->next){
if(cur1 != cur->random){
++count;
continue;
}
break;
}
if(cur1== NULL){
cur_new->random = NULL;
}else{
ComplexNode* cur1_new = new_head->next;
while(count--){
cur1_new = cur1_new->next;
}
cur_new->random = cur1_new;
}
cur = cur->next;
cur_new = cur_new->next;
}
return new_head;
}
void LinkListPushFront1(ComplexNode** head,ComplexNode* ret){
if(head == NULL){
return;
}
ret->next = (*head)->next;
(*head)->next = ret;
}
检测代码:
//复杂函数的检测函数
textCopyComplex(){
ComplexNode* head1 = (ComplexNode*)malloc(sizeof(ComplexNode));
LinkListPushFront1(&head1,CreateNewNode1('a'));
LinkListPushFront1(&head1,CreateNewNode1('b'));
LinkListPushFront1(&head1,CreateNewNode1('c'));
LinkListPushFront1(&head1,CreateNewNode1('d'));
head1->next->random = head1->next->next;
head1->next->next->random = head1->next;
head1->next->next->next->random = head1->next;
head1->next->next->next->next->random = head1->next->next->next;
ComplexNode* ret = CopyComplex(head1);
printf("\n*****CopyComplex***********\n");
ComplexNode* cur = ret->next;
while(cur!= NULL){
printf("%c ",cur->data);
cur = cur->next;
}
printf("\n");
cur = ret->next;
while(cur!= NULL){
if(cur->random == NULL){
printf("NULL ");
}else{
printf("%c ",cur->random->data);
}
cur = cur->next;
}
printf("\n");
}