⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 集合运算.cpp

📁 是在visual c++环境下实现的数据结构集合并交和差的运算
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>
typedef struct List//节点的定义
{
	int data;
	struct List *next;
}List,* Link;
Link CreateList(Link head)//创建链表
{
	head=(Link)malloc(sizeof(List));
	head->next=NULL;
	//Link pointor;
	while (1)
	{
		Link pointor=(Link)malloc(sizeof(List));
		cin>>pointor->data;
		if (pointor->data==0)
		{
			break;
		}
        pointor->next=head->next;
		head->next=pointor;
	}
	return head;
}
void PrintList(Link head)//输出集合
{
	Link newhead=head->next;
    do
    {
       cout<<newhead->data<<" ";
	   newhead=newhead->next;
    }while (newhead!=NULL);
	cout<<endl;
}
void SetIntersection(Link head1,Link head2)//集合的交集
{
   for(Link p=head1->next;p!=NULL;p=p->next)
   for(Link q=head2->next;q!=NULL;q=q->next)
   {
	   if (p->data==q->data)
	   {
		   cout<<p->data<<" ";
	   }
   }
   cout<<endl;
}
void SetAltogeter(Link head1,Link head2)//集合并集
{
	for (Link p=head1->next;p!=NULL;p=p->next)
	{
		
		 for (Link q=head2->next;q!=NULL;q=q->next)
		 {
			if(p->data!=q->data&&q->next==NULL)
			{
			  cout<<p->data<<" ";
			}
			if(p->data==q->data)
			{
				break;
			}
		 }
	}
		   PrintList(head2);
		   cout<<endl;
}
void SetMission(Link head1,Link head2)//集合的差
{
	cout<<"Head1-Head2:"<<endl;
	for (Link p=head1->next;p!=NULL;p=p->next)
	{
		
		 for (Link q=head2->next;q!=NULL;q=q->next)
		 {
			if(p->data!=q->data&&q->next==NULL)
			{
			  cout<<p->data<<" ";
			}
			if(p->data==q->data)
			{
				break;
			}
		 }
	}
	cout<<endl;
	cout<<"Head2-Head1"<<endl;
	for (Link m=head2->next;m!=NULL;m=m->next)
	{
		for (Link n=head1->next;n!=NULL;n=n->next)
		{
			if (m->data!=n->data&&n->next==NULL)
			{
				cout<<m->data<<" ";
			}
			if (m->data==n->data)
			{
				break;
			}
		}
	}
	cout<<endl;
}
void main()
{
	Link head1;
	Link head2;
    system("color 75");
        cout<<"                 ★---您好,欢迎进入集合运算系统!---★\n";
        cout<<"================================================================================\n";
        cout<<"◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n";
        cout<<" □  1.创建列表Head1"<<endl;
        head1=CreateList(head1);
        cout<<" □  2.创建列表Head2"<<endl;
		head2=CreateList(head2);
        cout<<" □  3.输出链表1"<<endl;
		PrintList(head1);
        cout<<" □  4.输出链表2"<<endl;
		PrintList(head2);
        cout<<" □  5.集合的交集"<<endl;
		SetIntersection(head1,head2);
        cout<<" □  6.集合的并集"<<endl;
		SetAltogeter(head1,head2);
        cout<<" □  7.集合的差"<<endl;
		SetMission(head1,head2);
        cout<<"================================================================================\n";
}//主函数结束

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -