当前位置:网站首页>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;
}
边栏推荐
- 001_ layout
- 004_ icon
- Matlab 2012a drawing line segment with arrow
- If mybaits cannot query the data, it can query how to change it in the database
- Write this number in C
- 33Mysql
- Simple implementation of unity object pool
- [graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network
- 8 — router
- Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
猜你喜欢

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

Knowledge payment cannot escape the essence of "anxiety"

Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
![Scala basics [introduction and installation]](/img/c5/9e62070719e1e0db29b0e44b0f0bc1.png)
Scala basics [introduction and installation]

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

33Mysql

013_ slider

8 — router

DTW learning (dynamic time warping) -- Thought and code implementation

DMX configuration
随机推荐
What problems can cloud storage architecture solve for Devops?
Let‘sPlayCurling
Unity2d-- add keys to animation and bind events
Record an oom exception in production
Tools and life services
Is it safe to open an account in Sinosteel futures?
假離婚變成真離婚,財產怎麼辦
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux
想转行,但不知道自己要做什么工作比较好?
Is online stock trading safe? Do you need to open an account for stock trading?
DMX的配置
Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
AI landing manufacturing: intelligent robots should have these four abilities
A summary of the quantification of deep network model
【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态
搞透AQS原理(流程图及同步队列图解)
DDoS threat situation gets worse
CheapSwap 协议的诞生
dhu编程练习
[naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework