复制复杂(多指针)链表

来源:互联网 发布:显示网络存在安全隐患 编辑:程序博客网 时间:2024/05/21 10:13

题目:

复杂链表的复制。 在此链表中,每个节点不光含有一个指向下一个节点的next指针,还有一个指针any,指向链表中任意节点或者 NULL。如图:

                    __________
                   V                  |
         A ---> B ---> C ---> D ---> E --->F
         |          |____^__________^   
         |__________|

方法一:

第一步:先复制原链表中的每一个节点。 

第二步:由于每一个节点上还有一个any指针需要填充,所以第二步依次遍历每一个节点在原链表中的位置,并依次填充。代码略。

此算法的复杂度:O(n^2)
算法二将对此做了优化,算法复杂度为:O(n)

方法二:

假设复杂链表用N表示,

第一步:给每一个N创建一个新的节点N',然后把创建好的每一个N'添加在N的后面。
第二步:通过遍历一次链表,将复制后的N'从长链表中分离出来,组成一个新的链表,新的链表即为复制后的复杂链表。 

此算法的复杂度:O(n)

代码:

#include <stdio.h>#include <stdlib.h>
//声明节点样子typedef struct node{char ch;struct node* next;struct node* any;}Node;//显示链表void show(Node* phead){Node* tmp = phead;while(tmp != NULL){printf("%c\t", tmp->ch);if(tmp->any != NULL)printf("%c any is: %c\n", tmp->ch, tmp->any->ch);else printf("%c any is NULL\n", tmp->ch);tmp = tmp->next;}printf("\n");}
//创建复杂链表Node* create(Node* phead, char ch){Node* tmp = malloc(sizeof(Node));tmp->ch = ch;tmp->next = NULL;tmp->any = NULL;Node* find = phead;if(phead == NULL)phead = tmp;else {while(find->next != NULL)find = find->next;find->next = tmp;}return phead;}
//给链表中的另一个指针any赋值void create_complex_list(Node* phead){Node* tmp = phead;tmp = tmp->next;Node* A = phead;A->any = tmp->next;Node* B = tmp;tmp = tmp->next->next;Node* D = tmp;D->any = B;tmp = tmp->next;B->any = tmp; }
//删除链表void destory(Node* phead){Node* tmp = NULL;while(phead){tmp = phead;phead = phead->next;free(tmp);}}
// 复制N节点为N',并加在N的后面,合成一条长链表
void clone_nodes(Node* phead){Node* clone = phead;if(phead == NULL){printf("Head is NULL. \n");exit(1);}while(clone != NULL){Node* tmp = malloc(sizeof(Node));tmp->ch = clone->ch;tmp->next = clone->next;tmp->any = clone->any;clone->next = tmp;clone = tmp;clone = clone->next;}}

//将长链表中的N'分离出来,形成新的复杂链表Node* separate_nodes(Node* phead){int count = 0 , i = 1;Node* newhead = NULL;Node* newtail = NULL;Node* tmp = phead;while(tmp != NULL){count++;tmp = tmp->next;} Node* find = phead;while(find != NULL){if(find->ch == find->next->ch){tmp = find->next;find->next = tmp->next;tmp->next = NULL;if(newhead == NULL){newhead = tmp;newtail = newhead;} else {newtail->next = tmp;newtail = newtail->next;}}find = find->next;}return newhead;}
int main(){Node* head = NULL;head = create(head, 'A');create(head, 'B');create(head, 'C');create(head, 'D');create(head, 'E');create(head, 'F');// 创建一个复杂的链表create_complex_list(head);printf("复制前: \n");show(head);      // 复制N节点为N',并加在N的后面,合成一条长链表clone_nodes(head);      /********************************************** 将长链表中的N'分离出来,合成一条短的链表,此       链表为复制后的复杂链表。      ***********************************************/Node* new = separate_nodes(head);printf("复制后: \n");show(new);destory(head);return 0;}

结果:



0 0
原创粉丝点击