当前位置:网站首页>DHU programming exercise
DHU programming exercise
2022-06-30 02:10:00 【qq_ forty-three million four hundred and three thousand six hun】
6 Find the intersection of sequences ( Linked list )
1. problem
Use the single linked list programming of the leading node :
There are two sequences , They represent two sets .
Find their intersection and output .
2. Enter description
The first line is the input sequence A Information about :
The first integer n(0<=n<=100), Expressing common ownership n Elements , After that n It's an integer , Express n Data of elements
The first line is the input sequence B Information about :
The first integer n(0<=n<=100), Expressing common ownership n Elements , After that n It's an integer , Express n Data of elements
The output shows that
Output a sequence of elements that intersect , See example for output format .
If the intersection is empty , The output “head–>tail”
The order of elements in the intersection , According to its sequence A Order in .
such as :
Sequence :
A:5 3 2 7
B:1 3 5 8
Then the intersection is 5 and 3, Because in the sequence A in ,5 stay 3 In front of , So in the intersection 5 Also in the 3 In front of .
3. Example
Input
4 5 3 2 7
4 1 3 5 8
Output
head–>5–>3–>tail
4. Code
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node* create(int n ,int num[])
{
int i;
struct node *current;// Current insertion position pointer
struct node *tail;// Tail pointer
struct node *head;// The head pointer
head = (struct node*)malloc(sizeof(struct node));// Head node
head->next = NULL;
current = head;// The current position pointer points to the header node
tail = head;// The tail pointer also points to the head node
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; // The current position is at the back , It needs to be modified tail The pointer
}
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);
// seek L1,l2 Intersection
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;// about L1 Every element in , All in L2 Find out if there are the same elements in , If you have any , Explain the intersection , Exit search directly , seek L1 The next element of is in L2 The corresponding point in
}
q = q->next;
}
p = p->next;// Description traversal L1 The next element in
q = L2->next;// The key point is , For each L1 The elements in , Every time I have to go through it from the beginning L2 Look for the same elements
}
cout << "-->tail" << endl;
return 0;
}
边栏推荐
- 云存储架构能解决 DevOps 的什么问题?
- After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
- C language number prime
- Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
- scp远程拷贝命令记录
- [MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux
- The odoo15 page jumps directly to the editing status
- [machine learning Q & A] cosine similarity, cosine distance, Euclidean distance and the meaning of distance in machine learning
- [MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce
- Is the processor the main factor in buying a mobile phone?
猜你喜欢

Unity2d-- add keys to animation and bind events

8 — router
![[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

Share the source code of the website of graduation student record

C language score ranking

Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?

当大学毕业感到迷茫怎么办?

003_ color

C language output integer in another format

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package
随机推荐
Leetcode 46 Full arrangement (February 15, 2022)
(4) Blender source code analysis flash window display process
The birth of the cheapswap protocol
DDoS threat situation gets worse
Fake divorce turns into real divorce. What about property
004_ icon
图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
Jenkins continuous integration environment construction VII (Jenkins parametric construction)
7 — filter
Geotools: common tools for mutual conversion of wkt, geojason, feature and featurecollection
DDoS threat situation gets worse
Radware warns about the next round of DDoS Attacks
Implementation of a simple camera based on pyqt5
AI landing manufacturing: intelligent robots should have these four abilities
9 — 正则校验集合
Method of converting songs from DTS to MP3
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
[graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network
The (3n+1) conjecture that C language kills people without paying for their lives
The largest DDoS attack ever peaked at 400 Gbps