当前位置:网站首页>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;
}
边栏推荐
- [naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework
- Is online stock trading safe? Do you need to open an account for stock trading?
- The (3n+1) conjecture that C language kills people without paying for their lives
- 假离婚变成真离婚,财产怎么办
- Three questions from the boss
- Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
- 【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
- Leetcode 46 Full arrangement (February 15, 2022)
- Configure cross domain requests
- [MySQL 06] backup and restore MySQL database in Linux + docker container environment
猜你喜欢

魔百盒CM201-2-CH-Hi3798MV300-300H-EMMC和NAND_红外蓝牙语音_通刷固件包

(4) Blender source code analysis flash window display process

JS reverse case -rus5 logic learning
![[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

ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display
![[mrctf2020]ezpop-1 | PHP serialization](/img/65/9b7a3ae3552d8784b0c77a1130d762.png)
[mrctf2020]ezpop-1 | PHP serialization

如何制作CSR(Certificate Signing Request)文件?
![[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

DMX configuration

widget使用setImageViewBitmap方法设置bug分析
随机推荐
如何制作CSR(Certificate Signing Request)文件?
DMX的配置
Record an oom exception in production
What are the payment and distribution systems?
Is it safe to open an account in Sinosteel futures?
Matlab 2012a 绘制带箭头的线段
Internet Crime Complaint Center reports an increase in DDoS Attacks
【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
AI落地制造业:智能机器人应具备这4种能力
8 — router
The national industrial information security development research center issued the report on industrial information security situation in 2021
DTW learning (dynamic time warping) -- Thought and code implementation
Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
If you want to install a set of monitoring, what is the process? How much is it?
After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
C language number prime
[graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network
Leetcode 46 Full arrangement (February 15, 2022)
Learning C language from scratch day 026
【二叉树】最大二叉树 II