c ++ 链表

Problem statement: Write a C++ program to find the union of two single linked lists.

问题陈述:编写一个C ++程序来查找两个单个链表的并集。

Example:

例:

    Let the first linked list be:
5->4->3->6->1->NULL
Let the second linked list to be:
3->8->9->12->1->NULL
So there union is:
5->4->3->6->1->8->9->12->NULL

Solution

Brute force approach:

蛮力法:

One technique can be to include all the nodes of a linked list and for other linked check whether nodes are already appended or not. Such an approach takes O(m*n) times complexity. m, n=length of linked lists.

一种技术可以是包括链接列表的所有节点,而对于其他链接,请检查节点是否已附加。 这种方法需要O(m * n)乘以复杂度mn =链表的长度

Efficient approach:

高效的方法:

Efficient approach use to use merge sort.

高效的方法用于使用合并排序

  1. Sort both the linked list using merge sort. ( for detailed refer to: Merge sort for single linked lists)

    使用合并排序对两个链表进行排序。 (有关详细信息,请参阅: 合并单个链接列表的排序 )

  2. Scan each linked list and build union according to following:

    扫描每个链表并根据以下内容建立并集:

  3. IF (list1->data < list2->data)
    Createnode(list1->data) && append node to the union list
    Traverse list1 by one step( list1=list1->next)
    ELSE IF(list1->data ==list2->data)
    Createnode(list1->data) && append nodeto the union list
    Traverse list1 by one step( list1=list1->next)
    Traverse list2 by one step( list2=list2->next)
    ELSE//list1->data >list2->data
    Createnode(list2->data) && append node to the union list
    Traverse list2 by one step( list2=list2->next)
    END IF-ELSE
    
    
  4. Display the union list

    显示联合列表

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C ++实现查找两个单个链表的并集 (C++ implementation to find Union of two single Linked Lists)

#include<bits/stdc++.h>
using namespace std;
class node{public:
int data; // data field
struct node *next;
};
void display(class node* head){node* current=head; // current node set to head
while(current!=NULL){ //traverse until current node isn't NULL
printf("%d ",current->data);
current=current->next; // go to next node
}
}
node* creatnode(int d){node* temp=(node*)malloc(sizeof(node));
temp->data=d;
temp->next=NULL;
return temp;
}
node* mergeList(node* split1,node* split2){node* newhead=NULL;
//base cases
if(split1==NULL)
return split2;
if(split2==NULL)
return split1;
if(split1->data<=split2->data){newhead=split1;
newhead->next=mergeList(split1->next,split2);
}
else{newhead=split2;
newhead->next=mergeList(split1,split2->next);
}
return newhead;
}
void splitList(node* head,node** split1,node** split2){node* slow=head;
node* fast=head->next;
while(fast!=NULL){fast=fast->next;
if(fast!=NULL){slow=slow->next;
fast=fast->next;
}
}
*split1=head;
*split2=slow->next;
//spliting
slow->next=NULL;
}
void mergeSort(node** refToHead){node* head=*refToHead;
node* split1,*split2;
//base case
if(head==NULL || head->next==NULL){return;
}
//split the list in two halves
splitList(head,&split1,&split2);
//recursively sort the two halves
mergeSort(&split1);
mergeSort(&split2);
*refToHead=mergeList(split1,split2);
return;
}
node* findUnion(node* head1, node* head2){if(head1==NULL && head2==NULL)
return NULL;
node* head3;
if(head1->data<head2->data){head3=creatnode(head1->data);
head1=head1->next;
}
else if(head1->data==head2->data){head3=creatnode(head1->data);
head1=head1->next;
head2=head2->next;
}
else{head3=creatnode(head2->data);
head2=head2->next;
}
node* temp=head3;
while( head1!=NULL && head2!=NULL){if(head1->data<head2->data){temp->next=creatnode(head1->data);
temp=temp->next;
head1=head1->next;
}
else if(head1->data==head2->data){temp->next=creatnode(head1->data);
temp=temp->next;
head1=head1->next;
head2=head2->next;
}
else{temp->next=creatnode(head2->data);
temp=temp->next;
head2=head2->next;
}
}
while(head1!=NULL){temp->next=creatnode(head1->data);
temp=temp->next;
head1=head1->next;
}
while(head2!=NULL){temp->next=creatnode(head2->data);
temp=temp->next;
head2=head2->next;
}
return head3;
}
int main(){printf("creating the linked list by inserting new nodes at the end\n");
printf("enter 0 to stop building the list, else enter any integer\n");
int k;
node* curr,*temp;
cout<<"enter list1...\n";
scanf("%d",&k);
node* head1=creatnode(k); //buliding list, first node
scanf("%d",&k);
temp=head1;
///inserting at the end//
while(k){curr=creatnode(k);
temp->next=curr;//appending each node
temp=temp->next;
scanf("%d",&k);
}
cout<<"displaying list1...\n";
display(head1); // displaying the list
cout<<"\nenter list2...\n";
scanf("%d",&k);
node* head2=creatnode(k); //buliding list, first node
scanf("%d",&k);
temp=head2;
///inserting at the end//
while(k){curr=creatnode(k);
temp->next=curr;//appending each node
temp=temp->next;
scanf("%d",&k);
}
cout<<"displaying list1...\n";
display(head2);
//sort both the lists
mergeSort(&head1);
mergeSort(&head2);
cout<<"\ndisplaying their union...\n";
node* head3=findUnion(head1,head2);
display(head3);
return 0;
}

Output

输出量

creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
enter list1...
5 4 3  6 1 0
displaying list1...
5 4 3 6 1
enter list2...
3 8 9 12 1 0
displaying list1...
3 8 9 12 1
displaying their union...
1 3 4 5 6 8 9 12
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Example with explanation:

带有说明的示例:

First linked list: 5->4->3->6->1->NULL
Second linked list:3->8->9->12->1->NULL
After applying merge sort:
First linked list: 1->3->4->5->6->NULL
Second linked list: 1->3->8->9->12->NULL
---------------------------------------------------------
//for better understanding nodes are represented only by respective values
head1=1
head2=1
FindUnion(1,1):
0th iteration
head1!=NULL&&head2!=NULL
head1->data==head2->data //=1
thus head3(head of union list) =1
temp=head3=1
union list up to now:
1->NULL
head1=1->next=3;
head2=1->next=3;
1st iteration
head1!=NULL&&head2!=NULL
head1->data==head2->data //=3
thus temp->next =3
temp=temp->next=3;
union list up to now:
1->3->NULL
head1=3->next=4;
head2=3->next=8;
2nd iteration
head1!=NULL&&head2!=NULL
head1->data<head2->data //4<8
thus temp->next =head1->data=4
temp=temp->next=4;
union list up to now:
1->3->4->NULL
head1=4->next=5;
3rd iteration
head1!=NULL&&head2!=NULL
head1->data<head2->data //5<8
thus temp->next =head1->data=5
temp=temp->next=5;
union list up to now:
1->3->4->5->NULL
head1=5->next=6;
4th iteration
head1!=NULL&&head2!=NULL
head1->data<head2->data //6<8
thus temp->next =head1->data=6
temp=temp->next=6;
union list up to now:
1->3->4->5->6->NULL
head1=6->next=NULL;
5th iteration
head1 ==NULL
head2!=NULL
thus temp->next =head2->data=8
temp=temp->next=8;
union list up to now:
1->3->4->5->6->8->NULL
head2=8->next=9;
6th iteration
head1 ==NULL
head2!=NULL
thus temp->next =head2->data=9
temp=temp->next=9;
union list up to now:
1->3->4->5->6->8->9->NULL
head2=9->next=12;
7th iteration
head1 ==NULL
head2!=NULL
thus temp->next =head2->data=12
temp=temp->next=12;
union list up to now: 1->3->4->5->6->8->9->12->NULL
head2=12->next=NULL;
8th iteration:
head1==NULL
head2==NULL
thus no more scanning, union list is built
union: 1->3->4->5->6->8->9->12->NULL
Head3 at 1

翻译自: https://www.includehelp.com/cpp-programs/find-union-of-two-single-linked-lists.aspx

c ++ 链表

c ++ 链表_C ++程序查找两个单个链表的并集相关推荐

  1. c++ 取两个链表的交集_使用C ++程序查找两个链表的交集

    c++ 取两个链表的交集 Problem statement: Write a C++ program to find the intersection of two single linked li ...

  2. 单链表操作之合并两个单链表

    ***单链表操作之合并两个单链表*** /*  本篇博客为合并两个单链表, 有序和无序都可以: 其中用到单链表的尾插和单链表的选择排序: @单链表排序之选择排序链接:http://blog.csdn. ...

  3. 用c 语言实现数组的并集,C ++程序查找两个未排序数组的并集和交集

    在本文中,我们将讨论一个程序来查找两个给定的未排序数组的并集和交集. 让我们用" A"和" B"表示两个数组.然后这些数组的并集用A∪B表示,它基本上是两个给定 ...

  4. bst 删除节点_C ++程序查找具有N个节点的BST数量(加泰罗尼亚编号)

    bst 删除节点 Problem statement: C++ program to find number of binary search trees with n nodes. 问题陈述: C ...

  5. 左神算法:两个单链表相交的一系列问题(链表是否有环 / 两无环链表是否相交 / 两有环链表是否相交)

    本题来自左神<程序员代码面试指南>"两个单链表相交的一系列问题"题目. 题目 在本题中,单链表可能有环,也可能无环.给定两个单链表的头节点 head1 和 head2, ...

  6. 单链表的顺序查找c语言,单链表(C语言)

    首先 顺序存储在左侧,链式存储在右侧 在使用数组实现序列表之前,先对序列表的优缺点进行总结,因为它易于查找,添加和删除都很复杂. 回顾线性表顺序存储的优缺点 可以说链表的特性恰好相反,易于添加和删除, ...

  7. neo4j 查询同一节点的两个上级_链表的魅力:两个单向链表的第一个交点

    最近听了左神的算法课,对一些常用数据结构以及算法改进的思路有了更深的理解,特此总结,不定期更新算法题目以及答案总结!笔者使用C++进行算法重现!虽然左神使用的是JAVA,但他自己也说了,算法与语言无关 ...

  8. 链表问题1——打印两个有序链表的公共部分

    题目 给定两个有序链表的头指针head1和head2,打印两个链表的公共部分. 思路 因为是有序链表假设从小到大,所以从两个链表的头开始进行如下判断: 如果head1的值小于head2,则head1往 ...

  9. python创建一个有序链表_Python实现合并两个有序链表的方法示例

    本文实例讲述了Python实现合并两个有序链表的方法.分享给大家供大家参考,具体如下: 思路:先选出第一个节点,然后遍历两个链表,把小的作为当前节点的下一个节点,一直到其中一个链表遍历完,这时候把另一 ...

最新文章

  1. Java中的文件路径
  2. uboot2014.10移植(一)
  3. 深入理解数据库行锁与表锁
  4. python在材料模拟中的应用_材料模拟python_模拟-python模拟-在不妨碍实现的情况下修补方法...
  5. MIP 官方发布 v1稳定版本
  6. (Deep learning)深度卷积网络实战——第四部分
  7. 判断手机是否支持闪光灯的
  8. 图片上传限制文件类型
  9. Axure获取焦点和触发事件
  10. 华为电脑安装双系统linux,华为MateBook笔记本安装Win10与Ubuntu双系统
  11. 服务器空文件夹无法删除怎么办,为什么文件夹删不掉
  12. 大连民族大学计算机科学学院,魏巍 - 大连民族大学 - 计算机科学与工程学院
  13. Facebook新模型SEER|图像预训练的内卷
  14. Beautifulsoup爬取起点中文网 保存到mysql
  15. opentstack使用keypair文件登陆实例
  16. ESP8266 对接RFID RC522 设备读取门禁卡
  17. 秘制牛肉团队博客目录
  18. CSS: overflow-anchor 固定滚动到底部,随着页面内容增多滚动条自己滚动展示最新的内容
  19. 洛谷 4828 Nagisa loves Tomoya 题解
  20. 2020-8-30 《明日方舟》python抽卡代码

热门文章

  1. java function void_Java8中你可能不知道的一些地方之函数式接口实战
  2. ajax实现向上正在加载,向上滚动或者向下滚动分页异步加载数据(Ajax + lazyload)
  3. oracle查询sql时间ain,Oracle SQL 时间查询
  4. php 验证码字体居中,自定义验证码图片的宽高后文本垂直水平居中[帝国cms ShowKey.php]-网站程序网...
  5. 设置linux拨号服务端,CentOS Linux上搭建PPPoE服务器及拨号设置
  6. 使用场景_天然气重卡使用痛点及应用场景研究
  7. oracle命令行安装
  8. oracle中修改process
  9. spring入门到放弃——spring事务管理
  10. 使用dropwizard(3)-加入DI-dagger2