Fxxking dataStructure_ 集合的交并差运算

这是一个C++程序,用于实现集合的交、并、差运算。程序首先定义了顺序表结构,然后提供了初始化、插入、查找、比较和打印等操作。用户可以输入两个集合,选择执行并集、交集或差集运算,并输出结果集合。
摘要由CSDN通过智能技术生成


#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100 //初始表空间大小
#define LISTINCREMENT 10 //表长增量
typedef int Status; 
typedef char ElemType; 
using namespace std;


typedef struct{
ElemType *elem; 
int length; 
int listsize; 
}SqList;
 
SqList La,Lb,Lc,Ld; 
 
/**构造一个空的线性表L**/
Status InitList_Sq(SqList &L)
{
//L.elem = new ElemType;
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(OVERFLOW); 
L.length = 0; 
L.listsize = LIST_INIT_SIZE; 
return OK;

/*插入新元素e*/
Status ListInsert_Sq(SqList &L,int i,ElemType e)
{
ElemType *newbase,*p,*q;
if(i < 1 || i > L.length + 1) return ERROR;
if(L.length >= L.listsize){ 
newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT) * sizeof(ElemType));
if(!newbase) exit(OVERFLOW); 
L.elem = newbase; 
L.listsize += LISTINCREMENT; 
}
q = &(L.elem[i - 1]); 
for(p = &(L.elem[L.length - 1]); p >= q; --p) 
*(p + 1) = *p; 
*q = e; 
++L.length; 
return OK;
}
 
/*创建并输入*/
void CreateList_Sq(SqList &L)
{
ElemType ch;
int inlist = FALSE,j;
while((ch) != '\n')
{
scanf("%c",&ch); 
for(j = 0; j < L.length; j++)
if(ch == L.elem[j])
{
inlist = TRUE;
break;
}
else
inlist = FALSE;
if(!inlist && ch != '\n') 
ListInsert_Sq(L,L.length+1,ch);
}
ch=0;
}
/*判断相等*/
Status Equal(ElemType a,ElemType b)
{
if(a == b) 
return TRUE;
else 
return FALSE;
}
 
/*查找元素并返回*/
int LocateElem_Sq(SqList L,ElemType e,Status(* compare)(ElemType,ElemType))
{
ElemType *p;
int i;
i = 1; 
p = L.elem; 
while(i <= L.length && !(* compare)(*p++,e))
++i;
if(i <= L.length)
return i;
else 
return 0; 

 
/*销毁*/
Status Clear_Sq(SqList &L)

ElemType elem;
free(L.elem);
L.elem = NULL;
return OK;
}
 
/*打印*/
void Print_Sq(SqList L){
int i;
for(i = 0; i < L.length; i++)
cout<<L.elem[i]<<endl;
if(L.length == 0) 
cout<<"空集"<<endl;
cout<<"集合中个数为"<<L.length<<endl;
}
 
/*求并集*/
void Union_Sq(SqList La,SqList Lb,SqList)
{
int i;
ElemType elem;
Lc.length=0;
for (i=0;i<=La.length;i++)
Lc.elem[Lc.length++]=La.elem[i];
for (i=1;i<=Lb.length;i++)
{
elem=Lb.elem[i-1];
if(!LocateElem_Sq(La,elem,Equal))//用Equal比较- -
ListInsert_Sq(Lc,Lc.length+1,elem);
}
}


/*求交集*/
void Mix_Sq(SqList La, SqList Lb, SqList &Lc)
{
int i;
ElemType elem;
Lc.length=0;
for (i=1;i<=La.length;i++)
{
elem=La.elem[i-1];
if (LocateElem_Sq(Lb,elem,Equal))//用Equal比较- -
ListInsert_Sq(Lc,Lc.length+1,elem);
}
}


/*求差集*/
void Minus_Sq(SqList La,SqList Lb, SqList &Lc)
{
int i;
ElemType elem;
Lc.length = 0;
for(i = 1; i <= La.length; i++)
{
elem = La.elem[i-1];
if(!LocateElem_Sq(Lb,elem,Equal))
ListInsert_Sq(Lc,Lc.length+1,elem);
   }
}


int main()
{
char s;
int l=1;

InitList_Sq(La);
cout<<"请输入第一个集合"<<endl;
CreateList_Sq(La);
Print_Sq(La);


InitList_Sq(Lb);
cout<<"请输入第二个集合"<<endl;
CreateList_Sq(Lb);
Print_Sq(Lb);

InitList_Sq(Lc);
cout<<"输入a进行集合的并运算,输入b进行集合的交运算,输入c进行集合的差运算"<<endl;
cin>>s;
switch(s)
{
case 'a': Union_Sq(La,Lb,Lc);
Print_Sq(Lc);
break;
case 'b': Mix_Sq(La,Lb,Lc);
Print_Sq(Lc);
break;
case 'c': Minus_Sq(La,Lb,Lc);
Print_Sq(Lc);
break;
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值