当前位置:网站首页>¥1-2 例2.2 将两个集合的并集放到线性表中
¥1-2 例2.2 将两个集合的并集放到线性表中
2022-07-25 09:19:00 【叶萧白】
题目描述

样例输入
2
1 3
3
5 3 7
样例输出
1 3 5 7
源代码
#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
const int MaxSize = 1000; //定义最大储存数
class SqList
{
public:
int data[MaxSize]; //结构体数组
int length; // 长度
};
void InitList(SqList*& L) //初始化线性表
{
L = (SqList*)malloc(sizeof(SqList)); //分配空间
L->length = 0; //使长度为0
}
void CreatList(SqList*& L) //建立线性表
{
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; //找到返回他的逻辑序号
}
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;
}
边栏推荐
- Numpy - 数组array的构造
- Difference between redis and mongodb (useful for interview)
- The interviewer asked: how to prevent oversold? There are several ways to achieve it
- 动态添加多tab,并初始化每个tab页面
- MySQL的索引、视图与事务
- excl批量导入数据,后台公共解析方法
- 『每日一问』volatile干嘛的
- matplotlib数据可视化三分钟入门,半小时入魔?
- Mongodb exploration phase [easy to understand]
- OverTheWire-Bandit
猜你喜欢

activemq--死信队列

『怎么用』装饰者模式

activemq--消息重试机制

yarn : 无法加载文件 yarn.ps1,因为在此系统上禁止运行脚本。

分布式一致性协议之Raft

Shell script

Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in

idea中将lib目录下的jar包加入到项目中

What are stand-alone, cluster and distributed?

Nacos搭建配置中心出现client error: invalid param. endpoint is blank
随机推荐
TCP网络应用程序开发流程
redis的五种数据结构原理分析
Detailed explanation of pipeline pipeline mechanism in redis
MySQL排序
数据分析之numpy基础包
activemq--可持久化机制之AMQ
excl批量导入数据,后台公共解析方法
C#语言和SQL Server数据库技术
Go基础2
一文搞懂try、catch、finally(包含return)执行流程(全网最详细解析)
activemq--可持久化机制
ActiveMQ -- JDBC code of persistent mechanism
『每日一问』LockSupport怎么实现线程等待、唤醒
『怎么用』代理模式
Redis数据库基础
The interviewer asked: how to prevent oversold? There are several ways to achieve it
Understand the execution process of try, catch and finally (including return) (the most detailed analysis of the whole network)
DVWA练习一 暴力破解
【线程知识点】-- 自旋锁
Numpy- array属性、改变形状函数、基本运算