当前位置:网站首页>¥ 1-2 example 2.2 put the union of two sets into the linear table
¥ 1-2 example 2.2 put the union of two sets into the linear table
2022-07-25 09:54:00 【Ye Xiaobai】
Put the union of two sets into a linear table
Title Description

The sample input
2
1 3
3
5 3 7
Sample output
1 3 5 7
Source code
#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
const int MaxSize = 1000; // Define maximum storage
class SqList
{
public:
int data[MaxSize]; // Array of structs
int length; // length
};
void InitList(SqList*& L) // Initialize linear table
{
L = (SqList*)malloc(sizeof(SqList)); // Allocate space
L->length = 0; // Make the length 0
}
void CreatList(SqList*& L) // Build a linear table
{
int n, i, e;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> e;
L->data[i] = e;
L->length++;
}
}
int ListLength(SqList* L)
{
return (L->length);
}
int LocateElem(SqList* L, ElemType e)
{
int i = 0;
while (i < L->length && L->data[i] != e)
i++;
if (i >= L->length)
return 0;
else
return i + 1; // Find the logical sequence number that returns him
}
bool GetElem(SqList* L, int i, ElemType& e)
{
if (i<1 || i>L->length) return false;
e = L->data[i - 1];
return true;
}
bool ListInsert(SqList*& L, int i, ElemType e)
{
int j;
if (i<1 || i>L->length+1 || L->length == MaxSize)
return false;
i--;
for (j = L->length; j > i; j--)
{
L->data[j] = L->data[j - 1];
}
L->data[i] = e;
L->length++;
return true;
}
void UnionList(SqList * & LA, SqList *& LB, SqList *& LC)
{
int i, lena;
InitList(LC);
ElemType e;
for (i = 1; i <= ListLength(LA); i++)
{
GetElem(LA, i, e);
ListInsert(LC, i, e);
}
lena = ListLength(LA);
for (i = 1; i <= ListLength(LB); i++)
{
GetElem(LB, i, e);
if (!LocateElem(LA, e))
{
ListInsert(LC, ++lena, e);
}
}
}
void DispList(SqList* L)
{
for (int i = 0; i < L->length; i++)
{
cout << L->data[i] << " ";
}
cout << endl;
}
int main()
{
SqList* L1, *L2, *L3;
InitList(L1);
CreatList(L1);
InitList(L2);
CreatList(L2);
UnionList(L1, L2, L3);
DispList(L3);
return 0;
}
边栏推荐
- 初识Opencv4.X----为图像添加高斯噪声
- [data mining] Chapter 2 understanding data
- Kotlin collaboration: foundation and use of collaboration
- Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 2)
- Introducing MLOps 解读(一)
- Sort out personal technology selection in 2022
- expect+sh实现自动交互
- Wechat applet realizes the rotation map (automatic switching & manual switching)
- VCS常用命令
- yolov5实现小数据集的目标检测--kolektor缺陷数据集
猜你喜欢

How to import a large amount of data in MATLAB

matlab如何导入大量数据

手持振弦采集仪对振弦传感器激励方法和激励电压

First knowledge of opencv4.x --- drawing shapes on images

【深度学习模型部署】使用TensorFlow Serving + Tornado部署深度学习模型

无向连通图邻接表的创建输出广度深度遍历

初识Opencv4.X----图像模板匹配

matlab绘图|坐标轴axis的一些常用设置

多通道振弦、温度、模拟传感信号采集仪数据查看和参数修改

How to import a large amount of data in MATLAB
随机推荐
Server CUDA toolkit multi version switching
Kotlin basic knowledge points
初识Opencv4.X----为图像添加高斯噪声
Get to know opencv4.x for the first time --- add salt and pepper noise to the image
CCF 201604-2 俄罗斯方块
【深度学习】卷积神经网络
服务器cuda toolkit多版本切换
CCF 201509-2 日期计算
Solve the problem that esp8266 cannot connect mobile phones and computer hotspots
Creation of adjacency table of undirected connected graph output breadth depth traversal
BSP3 电力监控仪(功率监控仪)端子定义和接线
First knowledge of opencv4.x --- image convolution
目标检测与分割之MaskRCNN代码结构流程全面梳理+总结
CDA Level1知识点总结之多维数据透视分析
Customize the view to realize the background of redeeming lottery tickets [elementary]
Bracket matching problem
First knowledge of opencv4.x --- image histogram equalization
CDA LEVELⅠ2021新版模拟题二(附答案)
A number converted from a decimal integer to another base
ADC简介