当前位置:网站首页>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;
}
边栏推荐
- DDoS threat situation gets worse
- If mybaits cannot query the data, it can query how to change it in the database
- 33Mysql
- Realization of a springboard machine
- How to create a CSR (certificate signing request) file?
- Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package
- 【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态
- [graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions
- Encapsulate a complete version of the uniapp image and video upload component, which can be used immediately, switch between images and videos, customize the upload button style, delete the button sty
- 一次 Keepalived 高可用的事故,让我重学了一遍它!
猜你喜欢

Knowledge payment cannot escape the essence of "anxiety"

013_ slider

The (3n+1) conjecture that C language kills people without paying for their lives

Unity2d-- add keys to animation and bind events

C language number prime

Conjecture of prime pairs in C language

Widget uses setimageviewbitmap method to set bug analysis

DTW学习(dynamic time warping)——思想、代码实现

Implementation of a simple camera based on pyqt5

33Mysql
随机推荐
ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display
Share the source code of the website of graduation student record
006_ radio
【二叉树】最大二叉树 II
Where can I find a pre training model for pytoch model training?
Summary of DOM
Let‘sPlayCurling
What is idempotency? Detailed explanation of four interface idempotence schemes!
DTW学习(dynamic time warping)——思想、代码实现
Internet Crime Complaint Center reports an increase in DDoS Attacks
Is the processor the main factor in buying a mobile phone?
AI落地制造业:智能机器人应具备这4种能力
Local page floating animation is realized with the help of scroll wheel
Radware warns about the next round of DDoS Attacks
Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
007_ checkbox
018_ rate
Scala basics [introduction and installation]
一种跳板机的实现思路