当前位置:网站首页>DHU programming exercise
DHU programming exercise
2022-06-30 02:10:00 【qq_ forty-three million four hundred and three thousand six hun】
7 Find the intersection of ordered sequences ( Linked list )
1. problem
Use the single linked list programming of the leading node :
There are two ordered sequences , They represent two sets .
Find their intersection and output .
Be careful : Here we need to use “ Orderly ” Characteristics of .
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
notes : Two sequences are input in order
3. 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”
4. Example
Input
4 1 3 5 7
4 1 4 5 8
Output
head–>1–>5–>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!=NULL&&q!=NULL)
{
if (p->data < q->data)
p = p->next;
else if (q->data < p->data) // Pay attention to the logic here ,if ,else if ,else
q = q->next;
else
{
cout << "-->" << p->data;
p = p->next;
q = q->next;
}
}
cout << "-->tail" << endl;
return 0;
}
边栏推荐
- 208. implement trie (prefix tree) - attach detailed notes
- Upload, use of Avatar
- 记录生产的一次OOM异常
- [graph neural network] summary of graph classification study [3]: evaluation of graph classification methods and future research directions
- The national industrial information security development research center issued the report on industrial information security situation in 2021
- Yyds dry inventory consistent and smooth zoom type and spacing
- Gesture digital enlightenment learning machine
- AI landing manufacturing: intelligent robots should have these four abilities
- ROS Bridge 笔记(01)— apt 安装、源码编译安装、安装依赖、运行显示
- Leetcode 46 Full arrangement (February 15, 2022)
猜你喜欢

widget使用setImageViewBitmap方法设置bug分析

搞透AQS原理(流程图及同步队列图解)

Matlab 2012a drawing line segment with arrow

Jenkins continuous integration environment construction VII (Jenkins parametric construction)

图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?

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

Learning C language from scratch day 026

CTF introductory learning (WEB direction)

33Mysql

Widget uses setimageviewbitmap method to set bug analysis
随机推荐
[MySQL 06] backup and restore MySQL database in Linux + docker container environment
A summary of the quantification of deep network model
C language I want to pass
What should I do when I feel confused after graduation from university?
dhu编程练习
018_ rate
How does payment splitting help B2B bulk commodity transactions?
Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
The largest DDoS attack ever peaked at 400 Gbps
002_ container
Who can use redis expired monitoring to close orders and get out of here!
33Mysql
As VoIP became the target, DDoS attacks surged by 35% in the third quarter
JS reverse case -rus5 logic learning
9 - regular check set
C language continues (3n+1) conjecture
Scala basics [introduction and installation]
26. common interview questions of algorithm
Geotools: common tools for mutual conversion of wkt, geojason, feature and featurecollection
What problems can cloud storage architecture solve for Devops?