数据结构课程设计报告集合的交并差运算.docx
《数据结构课程设计报告集合的交并差运算.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计报告集合的交并差运算.docx(24页珍藏版)》请在冰豆网上搜索。
![数据结构课程设计报告集合的交并差运算.docx](https://file1.bdocx.com/fileroot1/2022-10/17/b44c07b4-3371-405f-a7bc-81563ff2284d/b44c07b4-3371-405f-a7bc-81563ff2284d1.gif)
数据结构课程设计报告集合的交并差运算
淮阴工学院
数据结构课程设计报告
作者:
学号:
班级:
学院:
专业:
题目:
指导教师:
2016
年
1
月
1课题描述
编制一个能演示执行集合的交、并和差运算的程序。
集合元素用小写英文字母,执行各种操作应以对话方式执行。
利用单链表表示集合;理解好三种运算的含义
2系统设计
2.1功能模块设计
2.1.1基于单链表设计
(1)节点结构单元模块——定义有序表的节点结构;
typedefstructLNode//定义结构体类型指针
{chardata;
structLNode*next;
}*pointer;
(2)有序表单元模块——实现有序表的抽象数据类型;
readdata(pointerhead)
初始条件:
head是以head为头节点的空链表。
操作结果:
生成以head为头节点的非空链表。
pop(pointerhead)
初始条件:
head是以head为头节点的非空链表。
操作结果:
将以head为头节点的链表中数据逐个输出。
(3)集合单元模块——实现集合获得抽象数据类型;
and(pointerhead1,pointerhead2,pointerhead3)
初始条件:
链表head1、head2、head3已存在
操作结果:
生成一个由head1和head2的并集构成的集合head3。
or(pointerhead1,pointerhead2,pointerhead3)
初始条件:
链表head1、head2、head3已存在
操作结果:
生成一个由head1和head2的交集构成的集合head3。
(4)主程序模块
Voidmain(){
初始化;
do{
接受命令;
处理命令;
}while(“命令”!
=“退出”);}
2.1.2基于顺序表设计
(1)顺序表结构单元模块——定义顺序表的结构体;
typedefstruct//定义SeqList的结构体{
DataTypelist[MaxSize];
intsize;
}SeqList;
(2)函数单元模块——定义各种所需函数;
intListDel(SeqList*L,inti,DataType*x)//顺序表的删除函数
intListGet(SeqListL,inti,DataType*x)//获取顺序表的元素函数
voidListFind(SeqListL,DataTypex)//顺序表查找元素函数
voidSelcetSort(SeqList*L)//顺序表选择排序函数
voidUnionSet(SeqListmylist1,SeqListmylist2)//求并集函数
voidMixedSet(SeqListmylist1,SeqListmylist2)//求交集元素函数
voidDiffentSet(SeqListmylist1,SeqListmylist2)//求差集元素函数
(3)主函数单元模块——定义主函数;
voidmain()
{SeqListmylist1,mylist2;//定义顺序表mylist
inti;
DataTypetemp;
ListInitiate(&mylist1);
ListInitiate(&mylist2);}//初始化两个顺序表
2.2数据结构设计
2.2.1基于单链表设计
定义结构体类型指针,集合采用单链表存储。
typedefstructLNode//定义结构体类型指针
head1=(pointer)malloc(sizeof(structLNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(structLNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(structLNode));
2.1.2基于顺序表设计
typedefstruct//定义SeqList的结构体
{DataTypelist[MaxSize];
intsize;
voidUnionSet(SeqListmylist1,SeqListmylist2)//求并集
{
intm,i,j;
DataTypex;
SeqListTest;
ListInitiate(&Test);//定义并初始化
2.3算法设计
2.3.1基于单链表,顺序表设计
图2.1系统模块流程图
否
是
否是
图2.2主菜单流程图
否
是
否
是
图2.3并集模块流程图
求交集与差集的流程图与并集类似。
3详细设计
3.1菜单设计(基于单链表)
图3.1主界面
3.2源代码设计(基于单链表)
#include
#include
typedefstructLNode//定义结构体类型指针
{
chardata;
structLNode*next;
}*pointer;
voidreaddata(pointerhead)//定义输入集合函数
{
pointerp;
chartmp;
scanf("%c",&tmp);
while(tmp!
='\n')
{
p=(pointer)malloc(sizeof(structLNode));//为指针P申请内存空间
p->data=tmp;
p->next=head->next;
head->next=p;
scanf("%c",&tmp);
}
}
voidpop(pointerhead)//定义输出集合函数pop()出栈函数
{
pointerp;
p=head->next;
while(p!
=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
voidand(pointerhead1,pointerhead2,pointerhead3)//定义集合的并集函数
{
pointerp1,p2,p3;
p1=head1->next;
while(p1!
=NULL)//遍历链表head1
{
p3=(pointer)malloc(sizeof(structLNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
p1=p1->next;
}
p2=head2->next;
while(p2!
=NULL)//遍历链表head2
{
p1=head1->next;
while((p1!
=NULL)&&(p1->data!
=p2->data))
p1=p1->next;
if(p1==NULL)
{
p3=(pointer)malloc(sizeof(structLNode));
p3->data=p2->data;
p3->next=head3->next;
head3->next=p3;
}
p2=p2->next;
}}
voidor(pointerhead1,pointerhead2,pointerhead3)//定义集合的交集函数
{
pointerp1,p2,p3;
p1=head1->next;
while(p1!
=NULL)
{
p2=head2->next;
while((p2!
=NULL)&&(p2->data!
=p1->data))
p2=p2->next;
if((p2!
=NULL)&&(p2->data==p1->data))
{
p3=(pointer)malloc(sizeof(structLNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}}
voiddiffer(pointerhead1,pointerhead2,pointerhead3)//定义集合的差集函数
{
pointerp1,p2,p3;
p1=head1->next;
while(p1!
=NULL)
{
p2=head2->next;
while((p2!
=NULL)&&(p2->data!
=p1->data))
p2=p2->next;
if(p2==NULL)
{
p3=(pointer)malloc(sizeof(structLNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}}
voidmain()//主函数
{
intx;
printf("(输入数据,按回车键结束,第一个集合大于第二个集合)\n");
pointerhead1,head2,head3;
head1=(pointer)malloc(sizeof(structLNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(structLNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(structLNode));
head3->next=NULL;
printf("请输入集合1:
\n");
readdata(head1);//调用输入集合函数
printf("请输入集合2:
\n");
readdata(head2);//调用输入集合函数
A:
printf("1.并集2.交集3.差集4.结束x.重新运算\n");
do{
printf("请选择序号\n");
scanf("%d",&x);
switch(x)
{
case1:
printf("两集合的并是\n");
and(head1,head2,head3);//调用并集函数
pop(head3);
head3->next=NULL;
break;
case2:
printf("两集合的交是\n");
or(head1,head2,head3);//调用交集函数
pop(head3);
head3->next=NULL;
break;
case3:
printf("两集合的差是\n");
differ(head1,head2,head3);//调用差集函数
pop(head3);
head3->next=NULL;
break;
case4:
break;
default:
gotoA;
}
}while