当前位置:文档之家› 编制一个能演示执行集合的交、并和差运算的程序

编制一个能演示执行集合的交、并和差运算的程序

编制一个能演示执行集合的交、并和差运算的程序。

#include<iostream>
using namespace std;
typedef struct Node{
char data;
Node *next;
}Node,*LinkList;
#define SIZE sizeof(Node)
#define FALSE 0
#define TRUE 1
//初始化集合
void InitLinkList(LinkList Head)
{
char ch;Node *p=Head;
Head->next=NULL;
Head->data='\0';
cin>>ch;
while(ch!='#')
{
Node *newNode=(Node*)malloc(SIZE);
newNode->data=ch;
p->next=newNode;
p=p->next;
cin>>ch;
}
p->next=NULL;
}
//检查p1或p2所指向数据结点该不该加入到Head为起始的集合中^-^有点拗口,表达不是很好
int Check(char ch,LinkList Head)
{
Node *temp=Head->next;
int flag=TRUE;
while(temp!=NULL)
{
if(temp->data==ch){//不需要将数据插入
flag=FALSE;
return flag;
}
temp=temp->next;
}
return flag;
}
//合并两个集合
LinkList Merge(LinkList Head1,LinkList Head2) {
LinkList Head=(Node*)malloc(SIZE);
Head->data='\0';Head->next=NULL;
Node *p1=Head1->next;
Node *p2=Head2->next;
Node *p=Head;
while(p1!=NULL&&p2!=NULL)
{
if(p1->data==p2->data)
{
if(Check(p1->data,Head)==TRUE)
{
Node *newNode=(Node*)malloc(SIZE); newNode->data=p1->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
}
else
{
if(Check(p1->data,Head)==TRUE)
{
Node *newNode=(Node*)malloc(SIZE); newNode->data=p1->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
if(Check(p2->data,Head)==TRUE)
{
Node *newNode=(Node*)malloc(SIZE);
newNode->data=p2->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
}
p1=p1->next;
p2=p2->next;
}
while(p1!=NULL)
{
if(Check(p1->data,Head)==TRUE) {
Node *newNode=(Node*)malloc(SIZE); newNode->data=p1->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
p1=p1->next;
}
while(p2!=NULL)
{
if(Check(p2->data,Head)==TRUE) {
Node *newNode=(Node*)malloc(SIZE); newNode->data=p2->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
p2=p2->next;
}
return Head;
}
//集合A中的元素,B中是否存在
int IsExist(char data,LinkList Head) {
Node *p=Head->next;
int flag=FALSE;
while(p!=NULL)
{
if(p->data==data)
return flag=TRUE;
p=p->next;
}
return flag;
}
int IsExist2(char data,LinkList Head)
{
Node *p=Head->next;
int flag=FALSE;
while(p!=NULL)
{
if(p->data==data)
return flag;
p=p->next;
}
return flag=TRUE;
}
//两个集合的差集
LinkList Deprive(LinkList Head1,LinkList Head2) {
LinkList Head=(Node*)malloc(SIZE);
Node *p=Head;
Node *p1=Head1->next;
while(p1!=NULL)
{
if(IsExist2(p1->data,Head2)==1)
{
Node *newNode=(Node*)malloc(SIZE); newNode->data=p1->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
p1=p1->next;
}
return Head;
}
//两个集合交集
LinkList Insection(LinkList Head1,LinkList Head2)
{
Node *p1=Head1->next;
//Node *p2=Head2->next;
LinkList Head=(Node*)malloc(SIZE);
Head->data='\0';Head->next=NULL;
Node *p=Head;
while(p1!=NULL)
{
if(IsExist(p1->data,Head2)==1)
{
Node *newNode=(Node*)malloc(SIZE);
newNode->data=p1->data;
p->next=newNode;
p=newNode;
p->next=NULL;
}
p1=p1->next;
}
return Head;
}
//打印集合元素
void PrintLinkList(LinkList Head)
{
Node *p=Head->next;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
cout<<"\n";
}
int main()
{
char cmd;
do{
cout<<"输入两个集合的元素,输完一个集合的元素,按#结束\n";
LinkList head1=(Node*)malloc(SIZE); LinkList head2=(Node*)malloc(SIZE); InitLinkList(head1);InitLinkList(head2); Node *Head1=Merge(head1,head2); cout<<"两个集合合集为\n"; PrintLinkList(Head1);
Node *Head2=Insection(head1,head2); cout<<"两个集合交集为\n"; PrintLinkList(Head2);
Node *Head3=Deprive(head1,head2); cout<<"两个集合差集为\n"; PrintLinkList(Head3);
cout<<"按y/Y继续,否则结束\n"; cin>>cmd;
}while(cmd=='y'||cmd=='Y');
return 0;
}。

相关主题