作业帮 > 综合 > 作业

用单链表实现集合的并,交,差运算.急啊~~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语言的~
#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