- 1、本文档共5页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
有两个集合用两个线性表LA和LB表示即线性表中的数据元素即为集合中的成员。现要求一个新的集合A=Acup;B。
例2-1 假设有两个集合A和B分别用两个线性表LA和LB表示,即:线性表中的数据元素即为集合中的成员。现要求一个新的集合A=A∪B。
//////////////////////////////////////////////////////////
上述问题可演绎为:
要求对线性表作如下操作:
扩大线性表LA,将存在于线性表LB中而不存在于线性表LA中的数据元素插入到线性表LA中去。
//////////////////////////////////////////////////////////
操作步骤:
1.从线性表LB中依次察看每个数据元素;
GetElem(LB,i)→e
2.依值在线性表LA中进行查访;
LocateElem(LA,e,equal( ))
3.若不存在,则插入之。
ListInsert(LA,n+1,e)
//////////////////////////////////////////////////////////
void union(List La,List Lb){
La_len=ListLength(La);//求线性表的长度
Lb_len=ListLength(Lb);
for(i=1;i=Lb_len;i++){
GetElem(Lb,i,e);//取Lb中第i个数据元素赋给e
if(!LocateElem(La,e,equal()))
ListInsert(La,++La_len,e);
//La中不存在和e相同的数据元素,则插入之
}
}//union
//////////////////////////////////////////////////////////
#ifndef DSH
#define DSH
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef int ElemType;
#endif
//////////////////////////////////////////////////////////
#ifndef LISTH
#define LISTH
#include ds.h
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
}List; //俗称顺序表
#endif
Status InitList(List );
void CreateList(List , int[],int);
int ListLength(List);
void GetElem(List, int, ElemType );
int LocateElem(List, ElemType, Status (*compare)(ElemType,ElemType));
Status ListInsert(List , int, ElemType);
void PrintList(List);
////////////////////////////////////////////////////////////////
#include stdio.h
#include stdlib.h
#include List.h
Status InitList(List L)
{
//构造一个空的线性表L。
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
} //InitList
void CreateList(List L, int a[],int n)
{
//顺序输入n个数据元素,建立顺序表
int i;
for(i=0;in;++i)
L.elem[i]=a[i]; //输入元素值
L.length=n;
} //CreateList
int ListLength(List L)
{
return L.length;
}
void GetElem(List L, int i, ElemTy
文档评论(0)