集合的并交差运算数据结构课程设计.docx
《集合的并交差运算数据结构课程设计.docx》由会员分享,可在线阅读,更多相关《集合的并交差运算数据结构课程设计.docx(9页珍藏版)》请在冰豆网上搜索。
集合的并交差运算数据结构课程设计
集合的并交差运算数据结构课程设计
#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#defineOK1
#defineERROR0
#defineTRUE1
#defineFALSE0
#defineINFEASIBLE-1
#defineOVERFLOW-2
#defineNULL0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
typedefintStatus;
typedefcharElemType;
typedefstruct{
ElemType*elem;
intlength;
intlistsize;
}SqList;
StatusInitList(SqList&l)
{
l.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!
l.elem)exit(OVERFLOW);
l.length=0;
l.listsize=LIST_INIT_SIZE;
returnOK;
}
intListLength(SqListl)
{
return(l.length);
}
StatusListInsert_Sq(SqList&L,inti,ElemTypee){
//在顺序表L的第i个位置前插入元素e,i的合法值为1..L.length+1
if(i<1||i>L.length+1)
returnERROR;
if(L.length>=L.listsize)
{
ElemType*newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!
newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
ElemType*q=&L.elem[i-1],*p=&L.elem[L.length-1];
while(p>=q)
{
*(p+1)=*p;--p;
}//插入位置后的元素右移
*q=e;
++L.length;
returnOK;
}
StatusCreatSqList(SqList&l,ElemTypea[],intn)
{
intlen=ListLength(l);
for(inti=0;i<n;i++)
{
if(a[i]>='a'&&a[i]<='z')
ListInsert_Sq(l,++len,a[i]);
}
returnOK;
}
StatusGetElem(SqListL,inti,ElemType&e)
{
if(i<=0||i>L.length)
returnERROR;
else
e=*(L.elem+i-1);
returnOK;
}
Statusequal(ElemTypee1,ElemTypee2)
{
if(e1==e2)
returnTRUE;
else
returnFALSE;
}
intLocateElem_Sq(SqListL,ElemTypee,Status(*compare)(ElemType,ElemType))
{
ElemType*p=L.elem;//p指向第一个元素
inti=1;//i始终为p所指向元素的位序
while(i<=L.length&&!
(*compare)(*p++,e))
++i;
if(i<=L.length)
return(i);
else
return0;
}
StatusListDelete(SqList&L,inti,ElemType&e)
{
//在顺序表L中删除第i个元素,用e返回其值.
if(i<1||i>L.length)
returnERROR;//删除位置不合理
ElemType*p=&L.elem[i-1],*q=L.elem+L.length-1;
e=*p;
while(p<q){*p=*(p+1);++p;}//删除位置后的元素左移
--L.length;
returnOK;
}
voidUnion(SqList&La,SqListLb)
{
//将所有在线性表Lb中而不在La中的元素插入La
intla_len,lb_len;
ElemTypee;
la_len=ListLength(La);
lb_len=ListLength(Lb);
for(inti=1;i<=lb_len;++i)
{
GetElem(Lb,i,e);
if(LocateElem_Sq(La,e,equal)==0)
ListInsert_Sq(La,++la_len,e);
}
}
StatusJiaoJi(SqListl1,SqListl2,SqList&l3)
{
intl1_len,l2_len,l3_len,i=1,j=1;
ElemTypee,u;
l1_len=ListLength(l1);
l2_len=ListLength(l2);
l3_len=ListLength(l3);
for(i=1;i<=l1_len;i++)
{
GetElem(l1,
i,e);
for(j=l2_len;j>=1;j--)
{
GetElem(l2,j,u);
if(e==u)
{
ListInsert_Sq(l3,++l3_len,u);
break;
}
else
continue;
}
}
returnOK;
}
StatusChaJi(SqList&l1,SqListl2)
{
SqListlc;
intcount=0,lc_len,l1_len,l2_len;
ElemTypee,u,f;
InitList(lc);
JiaoJi(l1,l2,lc);
lc_len=ListLength(lc);
l1_len=ListLength(l1);
l2_len=ListLength(l2);
for(inti=1;i<=lc_len;i++)
{
GetElem(lc,i,e);
for(intj=1;j<=l1_len;j++)
{
GetElem(l1,j,u);
if(u==e)
{
ListDelete(l1,j,f);
}
}
}
returnOK;
}
voidOutputlist(SqList&L)
{
if(0==L.length)
printf("空集!
");
else
for(inti=0;i<L.length;++i)
{
printf("%c",*(L.elem+i));
}
}
voidmain()
{
system("@title集合的并交叉运算");
for(1;;)
{
system("colora1");
intc;
printf("********************************************************************\n");
printf("########执行程序:
1########退出程序:
2\n");
printf("********************************************************************\n");
printf("请按键选择:
");
scanf("%d",&c);
getchar();
printf("\n");
if(c==1)
{
SqListl1,l2,l3,la;
intn1,n2,i,j;
chara1[30],a2[30];
InitList(l1);
InitList(l2);
InitList(l3);
InitList(la);
printf("请输入第一个集合:
");
gets(a1);
n1=strlen(a1);
for(i=n1-1;i>=0;i--)//从最后一个开始依次与前面的比较重复赋值为0
{
for(j=0;j<i;j++)
{
if(a1[j]==a1[i])
a1[i]=0;
}
}
CreatSqList(l1,a1,n1);
la=l1;
printf("请输入第二个集合:
");
gets(a2);
n2=strlen(a2);
for(i=n2-1;i>=0;i--)//同上
{
for(j=0;j<i;j++)
{
if(a1[j]==a1[i])
a1[i]=0;
}
}
CreatSqList(l2,a2,n2);
printf("集合的交集是:
");
JiaoJi(l1,l2,l3);
Outputlist(l3);
printf("\n");
printf("集合的并集是:
");
Union(l1,l2);
Outputlist(l1);
printf("\n");
printf("集合的差集是:
");
ChaJi(la,l2);
Outputlist(la);
printf("\n\n**********************按任意键清屏!
*************************");
system("pause>null");
system("cls");
}
else
exit(0);
}
}