当前位置:网站首页>dhu编程练习
dhu编程练习
2022-06-30 02:08:00 【qq_43403657】
6 求序列的交集(链表)
1.问题
使用带头结点的单链表编程:
有两个序列,分别表示两个集合。
求它们的交集并输出。
2.输入说明
第一行输入序列A的信息:
第一个整数n(0<=n<=100),表示共有n个元素,其后有n个整数,表示n个元素的数据
第一行输入序列B的信息:
第一个整数n(0<=n<=100),表示共有n个元素,其后有n个整数,表示n个元素的数据
输出说明
输出交集的元素序列,输出格式见范例。
如果交集为空,则输出“head–>tail”
交集里的元素顺序,依照其在序列A中的顺序。
比如:
序列:
A:5 3 2 7
B:1 3 5 8
则交集为5和3,因为在序列A中,5在3的前面,所以在交集里5也在3的前面。
3.范例
输入
4 5 3 2 7
4 1 3 5 8
输出
head–>5–>3–>tail
4.代码
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node* create(int n ,int num[])
{
int i;
struct node *current;//当前插入位置指针
struct node *tail;//尾指针
struct node *head;//头指针
head = (struct node*)malloc(sizeof(struct node));//头结点
head->next = NULL;
current = head;//当前位置指针指向头结点
tail = head;//尾指针也指向头结点
for (i = 0; i < n; i++)
{
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
p->data = num[i];
p->next = current->next;
current->next = p;
current = p;
if (current->next == NULL) tail = current; //当前位置在最后面,则需要修改tail指针
}
return head;
}
int main()
{
int a[101], b[101] ,n,i,m;
struct node *L1, *L2;
cin >> n;
for (i = 0; i < n; i++)
cin >> a[i];
cin >> m;
for (i = 0; i < m; i++)
cin >> b[i];
L1 = create(n, a);
L2 = create(m, b);
//求L1,l2的交集
struct node *p, *q;
p = L1->next;
q = L2->next;
cout << "head";
while (p)
{
while (q)
{
if (p->data == q->data)
{
cout <<"-->"<< p->data;
break;//对于L1中的每个元素,都在L2中找是不是有相同元素,若有,说明相交,直接退出查找,寻找L1的下一个元素在L2中的对应点
}
q = q->next;
}
p = p->next;//说明遍历L1中的下一个元素
q = L2->next;//关键的一点,对于每个L1中的元素,每次都要从头遍历L2寻找是否有相同元素
}
cout << "-->tail" << endl;
return 0;
}
边栏推荐
- 【MySQL 06】linux + Docker容器环境中备份和还原MySQL数据库
- 谁再用Redis过期监听实现关闭订单,立马滚蛋!
- How to create a CSR (certificate signing request) file?
- Jenkins continuous integration environment construction VII (Jenkins parametric construction)
- The birth of the cheapswap protocol
- DDoS "fire drill" service urges companies to prepare
- 7 — filter
- Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
- DDoS attacks - are we really at war?
- [machine learning Q & A] cosine similarity, cosine distance, Euclidean distance and the meaning of distance in machine learning
猜你喜欢

018_ rate

DMX configuration

Unity2D--给动画添加关键帧并绑定事件

Write this number in C
![[MySQL 06] backup and restore MySQL database in Linux + docker container environment](/img/4e/8662d15ff84b2436d02948019540d3.png)
[MySQL 06] backup and restore MySQL database in Linux + docker container environment

DTW learning (dynamic time warping) -- Thought and code implementation

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package
![[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL](/img/37/d24c9e5fad606d2623900ad018b6af.png)
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
![[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars](/img/8f/c0cc1c8d19060a60d92c0d72f8b93d.png)
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars

当大学毕业感到迷茫怎么办?
随机推荐
Implementation of a simple camera based on pyqt5
26.算法常用面试题
Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
CheapSwap 协议的诞生
Understand AQS principle (flow chart and synchronous queue diagram)
Add a second network card (network interface NIC) for the virtual machine in azure portal in 2 minutes
C language output integer in another format
Upload, use of Avatar
Restore a 35k-55k Tencent Android Senior Engineer Interview
封装一个完整版的uniapp图片和视频上传组件,拿来即用,可进行图片视频切换,可自定义上传按钮样式,删除按钮样式,可单独上传图片或者视频,可限制上传数量
DTW学习(dynamic time warping)——思想、代码实现
Scala basics [introduction and installation]
[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce
【MySQL 05】SUSE 12 SP5 安装MySQL后第一次修改mysql密码
Some practical knowledge about PR
[MySQL 06] backup and restore MySQL database in Linux + docker container environment
Is it safe to open an account in Sinosteel futures?
Conjecture of prime pairs in C language
26. common interview questions of algorithm
Tencent released the first Office Photo 23 years ago. It's so chronological