当前位置:网站首页>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;
}
边栏推荐
- 什么是幂等性?四种接口幂等性方案详解!
- 003_ color
- UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了
- Add a second network card (network interface NIC) for the virtual machine in azure portal in 2 minutes
- Module import reload method
- 9 - regular check set
- 網上炒股安全麼?炒股需要開戶嗎?
- Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?
- After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
- 当大学毕业感到迷茫怎么办?
猜你喜欢

Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"

图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
![[MySQL 06] backup and restore MySQL database in Linux + docker container environment](/img/4e/8662d15ff84b2436d02948019540d3.png)
[MySQL 06] backup and restore MySQL database in Linux + docker container environment

Openlayers 3 built in interaction
![[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux

【银河麒麟V10】【桌面】火狐浏览器设置主页不生效

Conjecture of prime pairs in C language

C language I want to pass

一种跳板机的实现思路

The (3n+1) conjecture that C language kills people without paying for their lives
随机推荐
Widget uses setimageviewbitmap method to set bug analysis
当大学毕业感到迷茫怎么办?
Matlab 2012a drawing line segment with arrow
JS reverse case -rus5 logic learning
Repair method for win10 subsystem WSL if the passwords of root and other users are forgotten
DDoS threat situation gets worse
C language number prime
A keepalived high availability accident made me learn it again!
After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
(4) Blender source code analysis flash window display process
Openlayers 3 built in interaction
谁再用Redis过期监听实现关闭订单,立马滚蛋!
ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display
Oppo mobile phone search
Derivation of univariate polynomial in C language
dhu编程练习
Record an oom exception in production
DMX configuration
Matlab 2012a 绘制带箭头的线段