使用队列求集合的交集、并集、差集

代码:

#include <stdio.h>
#include <stdlib.h>

struct DataSetQueue 
{
	double val;
	DataSetQueue* next;
};


//显示队列
void show(struct DataSetQueue* head)
{
	struct DataSetQueue* p = head;
	while(p)
	{
		printf("%g ",p->val);
		p = p->next;
	}
	printf("\n");
}


//创建队列
struct DataSetQueue* CreateQueue()
{
	struct DataSetQueue *head = (struct DataSetQueue*)malloc(sizeof(DataSetQueue));
	head->next =0;
	return head;
}

//复制队列
struct DataSetQueue* Copy(struct DataSetQueue* head)
{
	struct DataSetQueue* phead,*t,*p1,*p2;
	phead = CreateQueue();
	phead->val = head->val;
	phead->next = 0;

	p1 = phead;
	p2 = head->next;
	while(p2)
	{
		t = (struct DataSetQueue*)malloc(sizeof(DataSetQueue));
		t->next = 0;
		t->val = p2->val;
		p1->next = t;
		p1 = t;
		p2 = p2->next;
	}
	return phead;
	
}

//添加到队列
void Push(struct DataSetQueue* head,double v)
{
	struct DataSetQueue* p = head;
	struct DataSetQueue* t = (struct DataSetQueue*)malloc(sizeof(DataSetQueue));
	t->val = v;
	t->next = 0;
	while(p->next)
		p = p->next;
	p->next = t;
}

//第一个元素弹出队列
struct DataSetQueue* Pop(struct DataSetQueue* head)
{
	struct DataSetQueue* p = head;
	head = head->next;
	return p;
}

//释放空间
void Release(struct DataSetQueue* head)
{
	struct DataSetQueue* p = head;
	while(head)
	{
		p = head->next;
		free(head);
		head = p;
	}
}


//判断是否在队列中
int isInQueue(struct DataSetQueue* head,double v)
{
	struct DataSetQueue* p = head;
	while(p)
	{
		if(p->val == v)
			return 1;
		else
			p = p->next;
	}
	return 0;
}


//从队列中删除某个元素
struct DataSetQueue* isInAndDel(struct DataSetQueue* head,double v)
{
	struct DataSetQueue* p1;
	struct DataSetQueue* t;
	if(head == 0)
		return 0;
	if (head->val == v)
	{
		p1 = head;
		head = head->next;
		free(p1);
		return head;
	}
	p1 = head;
	while(p1->next)
	{
		t = p1->next;
		if(t->val == v)
		{
			p1->next = t->next;
			free(t);
			return head;
		}else
			p1 = p1->next;
	}
	return 0;
}


//子集判定p2是否是p1的子集
int isSun(struct DataSetQueue* p1,struct DataSetQueue* p2)
{
	//有问题,需要修改
	struct DataSetQueue* t2;
	struct DataSetQueue* tmp;
	struct DataSetQueue* t1 = Copy(p1);
	t2 = p2;
	while(t2)
	{
		tmp = isInAndDel(t1,t2->val);
		if(tmp)
		{
			t1 = tmp;
			t2 = t2->next;
		}
		else
		{
			Release(t1);
			return 0;
		}
	}
	Release(t1);
	return 1;
}

//求交集
struct DataSetQueue* Jiaoji(struct DataSetQueue* p1,struct DataSetQueue* p2)
{
	struct DataSetQueue* head = 0;
	struct DataSetQueue* tmp;
	struct DataSetQueue* t1 = Copy(p1);
	struct DataSetQueue* t2 = p2;
	while(t2)
	{
		tmp = isInAndDel(t1,t2->val);
		if (tmp)
		{
			t1 = tmp;
			if(head == 0)
			{
				head = CreateQueue();
				head->val = t2->val;
			}else
			{
				Push(head,t2->val);
			}
		}
		t2 = t2->next;
	}
	Release(t1);
	return head;
}

//求并集
struct DataSetQueue* Bingji(struct DataSetQueue* p1,struct DataSetQueue* p2)
{
	struct DataSetQueue* head,*t;
	struct DataSetQueue* tmp;
	struct DataSetQueue* t2 = p2;
	head = Copy(p1);
	while(t2)
	{
		Push(head,t2->val);
		t2 = t2->next;
	}

	//减去两者的交集
	t = Jiaoji(p1,p2);
	while(t)
	{
		tmp = isInAndDel(head,t->val);
		if(tmp)
			head = tmp;
		t = t->next;
	}
	Release(t);
	return head;
}
//求差在p1中,但不在p2中
struct DataSetQueue* Chaji(struct DataSetQueue* p1,struct DataSetQueue* p2)
{
	struct DataSetQueue* tmp;
	struct DataSetQueue* head = Copy(p1);
	struct DataSetQueue* t2 = p2;
	while(t2)
	{
		tmp = isInAndDel(head,t2->val);
		if(tmp)
			head = tmp;
		t2 = t2->next;
	}
	return head;
}


int main()
{
	int i;
	int a[]={1,2,3,4,5,6,7};
	int b[]={3,6,9,11};
	int c[]={1,2};
	struct DataSetQueue* h1,*h2,*h3,*jj,*bj,*cj;
	h1 = CreateQueue();
	h2 = CreateQueue();
	h3 = CreateQueue();

	h1->val = a[0];
	for (i=1;i<7;i++)
		Push(h1,a[i]);
	
	h2->val = b[0];
	for (i=1;i<4;i++)
		Push(h2,b[i]);

	h3->val = c[0];
	for(i=1;i<2;i++)
		Push(h3,c[i]);

	//显示结合
	printf("集合1:");
	show(h1);
	printf("集合2:");
	show(h2);
	printf("集合3:");
	show(h3);
	
	//判断一个数是否在集合中
	if(isInQueue(h1,3))
		printf("3在集合1中\n");
	else
		printf("3不在集合1中\n");
	if(isInQueue(h2,3))
		printf("3在集合2中\n");
	else
		printf("3不在集合2中\n");

	//判断是否属于
	if (isSun(h1,h2))
		printf("集合2属于集合1\n");
	else
		printf("集合2不属于集合1\n");

	if (isSun(h1,h3))
		printf("集合3属于集合1\n");
	else
		printf("集合3不属于集合1\n");


	jj = Jiaoji(h1,h2);
	printf("集合1与集合2的交集:");
	show(jj);

	bj = Bingji(h1,h2);
	printf("集合1与集合2的并集:");
	show(bj);

	cj = Chaji(h1,h2);
	printf("集合1与集合2的差集:");
	show(cj);

	Release(h1);
	Release(h2);
	Release(h3);
	Release(jj);
	Release(bj);
	Release(cj);
	return 0;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qfl_sdu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值