当前位置:网站首页>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;
}
边栏推荐
- 207. curriculum - graph theory, depth traversal
- DDoS "fire drill" service urges companies to prepare
- Let‘sPlayCurling
- Learning C language from scratch day 026
- Oppo mobile phone search
- Record an oom exception in production
- DDoS attacks - are we really at war?
- Que se passe - t - il si un faux divorce devient un vrai divorce?
- dhu编程练习
- 云存储架构能解决 DevOps 的什么问题?
猜你喜欢

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package

Implementation of a simple camera based on pyqt5

搞透AQS原理(流程圖及同步隊列圖解)

26. common interview questions of algorithm

C language output integer in another format

8 — router

DMX configuration

UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了

What is idempotency? Detailed explanation of four interface idempotence schemes!

DTW学习(dynamic time warping)——思想、代码实现
随机推荐
Openlayers 3 built in interaction
Scala基础【入门及安装】
Implementation of a simple camera based on pyqt5
CTF introductory learning (WEB direction)
谁再用Redis过期监听实现关闭订单,立马滚蛋!
Realization of a springboard machine
004_ icon
Want to change careers, but don't know what you want to do?
Jenkins continuous integration environment construction VII (Jenkins parametric construction)
C language output integer in another format
图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
Scala basics [introduction and installation]
Knowledge payment cannot escape the essence of "anxiety"
003_ color
Let‘sPlayCurling
dhu编程练习
【MySQL 06】linux + Docker容器环境中备份和还原MySQL数据库
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux
Is it safe to open an account in Sinosteel futures?