《集合的并交差运算数据结构课程设计.docx》由会员分享,可在线阅读,更多相关《集合的并交差运算数据结构课程设计.docx(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、集合的并交差运算 数据结构课程设计 #include<string.h>#include<stdio.h>#include<malloc.h>#include<stdlib.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define INFEASIBLE -1#define OVERFLOW -2#define NULL 0#define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 typedef int Status;typede
2、f char ElemType;typedef structElemType *elem; int length; int listsize;SqList;Status InitList(SqList &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;int ListLength(SqList l)return(l.length);Status ListInsert_Sq
3、(SqList &L,int i, ElemType e)/在顺序表L的第i个位置前插入元素e,i的合法值为1.L.length+1if(i<1|i>L.length+1) return ERROR; 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;El
4、emType *q=&L.elemi-1, *p=&L.elemL.length-1;while(p>=q) *(p+1)=*p; -p; /插入位置后的元素右移*q=e;+L.length;return OK;Status CreatSqList(SqList &l,ElemType a,int n)int len=ListLength(l);for(int i=0;i<n;i+)if(ai>='a'&&ai<='z')ListInsert_Sq(l,+len,ai);return OK;Stat
5、us GetElem(SqList L,int i,ElemType &e)if(i<=0|i>L.length)return ERROR;elsee=*(L.elem+i-1);return OK;Status equal(ElemType e1,ElemType e2)if(e1=e2)return TRUE; else return FALSE; int LocateElem_Sq(SqList L, ElemType e, Status (*compare)(ElemType,ElemType)ElemType *p=L.elem; /p指向第一个元素int i=1
6、; /i始终为p所指向元素的位序while(i<=L.length&&!(*compare)(*p+,e)+i;if(i<=L.length) return(i);else return 0;Status ListDelete(SqList &L,int i,ElemType &e)/在顺序表中删除第个元素,用返回其值if(i<1|i>L.length) return ERROR;/删除位置不合理ElemType *p=&L.elemi-1,*q=L.elem+L.length-1;e=*p;while(p<q)*p=*(p
7、+1); +p; /删除位置后的元素左移-L.length;return OK;void Union(SqList &La,SqList Lb)/将所有在线性表Lb中而不在La中的元素插入Laint la_len , lb_len;ElemType e;la_len=ListLength(La);lb_len=ListLength(Lb);for(int i=1;i<=lb_len;+i)GetElem(Lb,i,e);if(LocateElem_Sq(La,e,equal)=0)ListInsert_Sq(La,+la_len,e);Status JiaoJi(SqList l
8、1,SqList l2, SqList &l3)int l1_len, l2_len,l3_len,i=1,j=1;ElemType e,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;elsecontinue;return OK;Status ChaJi(S
9、qList &l1,SqList l2)SqList lc;int count=0,lc_len,l1_len,l2_len;ElemType e,u,f;InitList(lc);JiaoJi(l1,l2,lc);lc_len=ListLength(lc);l1_len=ListLength(l1);l2_len=ListLength(l2);for(int i=1;i<=lc_len;i+)GetElem(lc,i,e);for(int j=1;j<=l1_len;j+)GetElem(l1,j,u);if(u=e)ListDelete(l1,j,f);return O
10、K;void Outputlist(SqList &L)if(0=L.length)printf("空集!");elsefor(int i=0;i<L.length;+i)printf("%c",*(L.elem+i);void main()system("title 集合的并交叉运算");for(1;)system("color a1");int c;printf("*n");printf(" # 执行程序: 1 # 退出程序: 2n");printf(&qu
11、ot;*n");printf("请按键选择: ");scanf("%d",&c);getchar();printf("n");if(c=1)SqList l1,l2,l3,la;int n1,n2,i,j;char a130, a230;InitList(l1);InitList(l2);InitList(l3);InitList(la);printf("请输入第一个集合: ");gets(a1);n1=strlen(a1);for(i=n1-1;i>=0;i-) /从最后一个开始依次与前面
12、的比较 重复赋值为0for(j=0;j<i;j+)if(a1j=a1i)a1i=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(a1j=a1i)a1i=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("nn*按任意键清屏!*");system("pause>null");system("cls");elseexit(0);