用单链表实现集合的并,交,差运算.急啊~~3号之前要.
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/14 08:17:48
用单链表实现集合的并,交,差运算.急啊~~3号之前要.
数据结构的实验
1、用有序单链表表示集合,在建立单链表时,对于每一个新结点都要在现有的链表中判断它的插入位置,如此,即可建立一个有序的单链表, 同时,舍去相同的字符.
2、集合的元素限定为小写字母字符[‘a’……’z’],
3、要求实现的算法:
1)两个集合的并运算
例如:text1=”space” , text2 = “status”
text1 ∪ text2 = “acepstu”
2)两个集合的交运算
例如:text1=”space” , text2 = “status”
text1 ∩ text2 = “as”
3)两个集合的差运算
例如:text1=”space” , text2 = “status”
text1 - text2 = “cep”
是用C语言的~
数据结构的实验
1、用有序单链表表示集合,在建立单链表时,对于每一个新结点都要在现有的链表中判断它的插入位置,如此,即可建立一个有序的单链表, 同时,舍去相同的字符.
2、集合的元素限定为小写字母字符[‘a’……’z’],
3、要求实现的算法:
1)两个集合的并运算
例如:text1=”space” , text2 = “status”
text1 ∪ text2 = “acepstu”
2)两个集合的交运算
例如:text1=”space” , text2 = “status”
text1 ∩ text2 = “as”
3)两个集合的差运算
例如:text1=”space” , text2 = “status”
text1 - text2 = “cep”
是用C语言的~
#include
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)
{
coutnext;
}
cout
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)
{
coutnext;
}
cout
数据结构与算法实习题介绍:A和B的并集:A和B的交集:A和B的差集:实现2个集合的并、交和差的运算.提示:集合的数据元素
数据结构 用c语言写的 集合的并、交和差运算的程序
编写一个C++程序,让计算机来完成两个集合的交、并、差、笛卡儿积运算
借助什么求集合的运算的交,并,补
集合的交、并、补定义及运算的列表?
pascal语言 编写一个算法实现两个集合的差运算
编写程序,实现两个集合的交运算(用C语言)
c语言 集合的差,交,并,笛卡尔积的运算.例如A={2,3,4,5,6,7,8}B={2,3,4,5,11,25}
用顺序表和单链表分别实现求集合的并集、交集和差集.
关系代数的5种基本运算是:并 差 选择 投影 笛卡尔积 ,还是 并 差 交 选择 投影?
集合运算里面“~∪是并,∩是交,但是~
本人碰见一道C语言难题,利用C语言实现:求任意两个集合的交集、并集、差集,