集合的相关操作

来源:互联网 发布:mac不能拷贝文件到u盘 编辑:程序博客网 时间:2024/06/05 20:50

#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 range(LinkList Head)
{  
char temp;
Node *q;
Node *p=Head->next;
while(p!=NULL)
{  
   q=p->next;
   while(q!=NULL)
   {
    if(p->data>q->data)
    {   temp=p->data;
     p->data=q->data;
     q->data=temp;
    }
    q=q->next;
   }
   p=p->next;
}
}


//打印集合元素
void PrintLinkList(LinkList Head)
{
Node *p=Head->next;
while(p!=NULL)
{
cout<<" ",
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);
range(head1);
cout<<"输出集合1排序后的链表/n";
PrintLinkList(head1);
range(head2);
cout<<"输出集合2排序后的链表/n";
PrintLinkList(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/N结束/n";
cin>>cmd;
}while(cmd=='y'||cmd=='Y');
return 0;
while(cmd=='n'||cmd=='N');
exit(0);
}

原创粉丝点击