当前位置:网站首页>7-51 combination of two ordered linked list sequences
7-51 combination of two ordered linked list sequences
2022-07-07 22:44:00 【Qingshan's green shirt】
7-51 The combination of two ordered list sequences
subject
Two non descending linked list sequences are known S1 And S2, Design functions to construct S1 And S2 New non descending list after merging S3.
Input format :
The input is divided into two lines , In each row, a non descending sequence composed of several positive integers is given , use −1 Represents the end of a sequence (−1 Not in this sequence ). The numbers are separated by spaces .
Output format :
Output the new non descending linked list after merging in one row , Separate numbers with spaces , There can't be extra spaces at the end ; If the new list is empty , Output NULL.
sample input :
1 3 5 -1 2 4 6 8 10 -1
No blank lines at the end
sample output :
1 2 3 4 5 6 8 10
No blank lines at the end
A lot of information , Some places refer to the ideas of bloggers .
Specific ideas
1. Set up two linked lists L1,L2, There is value , To -1 stop it .
2. Initialize the third linked list L3, Sequential comparison L1,L2 The value of the inside , Use L1,L2 The nodes inside construct the third linked list .( Because it is not said to create a new linked list and put the value after comparing the size , This is better )
3. Print L3.
Code implementation
#include<iostream>
#include<malloc.h> // This can't be lost
using namespace std;
typedef struct LNode {
int data;
struct LNode* next;
}LNode, *LinkList;
// How to create multiple linked lists
LinkList CreateList() // Initialization and creation are combined !
{
LinkList L = (LNode*)malloc(sizeof(LNode)); // Create a header node for the first linked list
L->next = NULL; // Initially empty
LinkList p;
p = L;
int e;
while (cin >> e)
{
if (e == -1)
break; *// I can't write this as return use return You must return the corresponding value
// use break* Jump out of current loop !
// Tail connection
LinkList temp = (LNode*)malloc(sizeof(LNode));
temp->data = e;
temp->next = p->next;
p->next = temp;
p = p->next;
}
return L; // At the end of the cycle return true Otherwise, it will jump out of the cycle And don't forget to add
}
// Merge functions
LinkList Merge(LinkList L1, LinkList L2) // The whole incoming
{
// initialization p3
LinkList p3;
LinkList L;
L = (LNode*)malloc(sizeof(LNode));
p3 = L;
p3->next = NULL;
LNode* p1 = L1->next; // Set up a pointer to traverse L1,L2
LNode* p2 = L2->next;
while (p1 && p2)
{
//LinkList temp = (LNode*)malloc(sizeof(LNode));
// Not quite right , The requirement is to merge , There is no requirement to create a new linked list .
if (p1->data >= p2->data) // Just put in the equal !
{
p3->next = p2; // Descending order Lenient !
p2 = p2->next; // Analogy bubble sort !!!
}
else {
p3->next = p1;
p1 = p1->next;
}
p3 = p3->next;
}
// After one traversal, connect the other nodes of the list that have not been traversed to the back
p3->next = p1 ? p1 : p2; // The trinocular operator is very simple , The following code can also be implemented , Better understanding
// while (p1)
//{
// p3->next = p1;
// p1 = p1->next;
// p3 = p3->next;
//}
//while (p2)
//{
// p3->next = p2;
// p2 = p2->next;
// p3 = p3->next;
//}
L1->next = NULL; // This is an important step ! Let the whistle node point to empty , Disconnect from the two linked lists
L2->next = NULL;
return L;
}
// Print L3
bool Print(LinkList &L3)
{
LNode* p = L3;
p = p->next;
if(p==NULL)
{
cout << "NULL" << endl;
return true; // Write here false still true It doesn't matter !
// break; no way Can only block circulation !
}
while (p != NULL)
{
cout << p->data << " " ;
p = p->next;
if(p->next == NULL) // Here we use the pointer to remove the extra space at the end !
{
cout << p->data;
//return true; // This one will do !
break; // You can use it here break!!!
}
// cout << endl;
}
return true; // After the cycle ends return true!
}
int main()
{
LinkList L1,L2,L3; // You can change three lines into one line
L1 = CreateList();
L2 = CreateList();
/* CreateList(L1); CreateList(L2);*/ // Such similar code cannot Ambiguity !
L3 = Merge(L1, L2);
Print(L3);
return 0;
}
Some of my questions
1. How to create multiple linked lists
LinkList CreateList(), Last return L;
Main function L1 = CreateList(); L2 = CreateList(); that will do
2. How to input these numbers in sequence until -1 stop it
while(1) or while(cin >> e) To -1 Then use break
3. How to compare two single linked lists data The value of the field is stored in the linked list L3 in ?
Set two more movable pointers to traverse
4. What about equal data ?
Just put it in ! Just put one !
5. What if one of them finishes traversing ?
p1,p2 Itself is in descending order , Don't bother. , Directly connect the remaining nodes of the linked list to the back
6. How can I even ?
Use the ternary operator !! Or loop
7. summary
(1)break Usage of : Terminate the current loop .
(2) Note whether the title requires the creation of a new linked list .
(3) Use the pointer p->next = NULL To remove the last blank .
(4) After combining to form the third linked list, let the first two linked lists point to null !
(5) The idea of creating a pointer to a linked list is very important , Traversing the linked list , Delete node , It's useful to find a node !
End of the flower ~!
2021.10.2
22:25
边栏推荐
- Understand the autograd package in pytorch
- Build an "immune" barrier in the cloud to prepare your data
- JS number is insufficient, and 0 is added
- #DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
- DBSync新增对MongoDB、ES的支持
- “拧巴”的早教行业:万亿市场,难出巨头
- Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
- Revit secondary development - Hide occlusion elements
- PHP records the pitfalls encountered in the complete docking of Tencent cloud live broadcast and im live group chat
猜你喜欢

XMIND mind mapping software sharing

Use blocconsumer to build responsive components and monitor status at the same time

Pdf document signature Guide
![[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster](/img/b6/e5d525d9c7c28f6ef04c3f59b40eb3.png)
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster

Remember an experience of using selectmany

微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹

What does it mean to prefix a string with F?

Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code

C # realizes the communication between Modbus protocol and PLC

How to choose the appropriate automated testing tools?
随机推荐
OpenGL homework - Hello, triangle
Record a garbled code during servlet learning
IP network active evaluation system -- x-vision
Two kinds of updates lost and Solutions
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
100million single men and women "online dating", supporting 13billion IPOs
微服务架构开源框架详情介绍
Loki, the "open source star picking program", realizes the efficient management of harbor logs
C development -- WPF simple animation
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Take full control! Create a "leading cockpit" for smart city construction
Redis官方ORM框架比RedisTemplate更优雅
[advanced MySQL] index details (I): index data page structure
Unity local coordinates and world coordinates
Oracle advanced (VI) Oracle expdp/impdp details
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
Understand the autograd package in pytorch
OpenGL configuration vs2019
Redis集群安装