list.h 代码:
/*
*Copyright (c) 2017,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:
*作 者:陈军正
*完成日期:2017年9月20日
*版 本 号:v1.0
*
*问题描述:假设有两个集合A和B分别用两个线性表LA和LB表示,即线性表中的数据元素即为集合中的成员。
*设计算法,用函数unionList(ListLA,ListLB,List&LC)函数实现该算法,
*求一个新的集合C=A∪B,即将两个集合的并集放在线性表LC中。
*/
#ifndef LIST_H_INCLUDEDbool ListInsert(SqList *&L,int i,ElemType e)
#define LIST_H_INCLUDED
#define MaxSize 50
typedef struct
{
int data [MaxSize];
int length;
}SqList;
void CreateList(SqList *&,int [],int );
void DispList(SqList *&);
void Add (SqList *&L1,SqList *&L2,SqList *&L);
#endif // LIST_H_INCLUDED
main.cpp 代码:
#include <iostream>
#include "list.h"
using namespace std;
int main ()
{
int x[3] = {1,2,3};
int y[3] = {4,5,6};
int z[6] = {0,0,0,0,0,0};
SqList *sq1,*sq2,*sq;
CreateList(sq1,x,3);
CreateList(sq2,y,3);
CreateList(sq,z,6);
cout<<"第一个集合:";
DispList(sq1);
cout<<endl<<"第二个集合:";
DispList(sq2);
Add (sq1,sq2,sq);
cout<<endl;
DispList(sq);
return 0;
}
#include "malloc.h"
#include "list.h"
#include <iostream>
using namespace std;
void CreateList(SqList *&L,int a[],int n)
{
int i;
L = (SqList *)malloc(sizeof(SqList));
for (i = 0;i<n;i++)
{
L->data[i] = a[i];
}
L->length = n;
}
void DispList(SqList *&L)
{
int i;
for(i=0;i<L->length;i++)
{
cout<<L->data[i]<<" ";
}
}
void Add (SqList *&L1,SqList *&L2,SqList *&L)
{
int i,q;
for (i = 0;i<L1->length;i++)
L->data[i] = L1->data[i];
for (q=0;q<L2->length;q++,i++)
L->data[i] = L2->data[q];
}
运行结果:
知识点总结:调用我们早已经写好的算法库,可以省掉很多时间,这大大提高了我们敲代码的效率,所以,注意自己早已写的代码的保存。