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

📄 集合交和并.cpp

📁 数据结构求取交并的程序
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
struct list
{
	    int first;
	    list *next;
};
        list* setup(list *nn)
{ 
	    list  *head;
	    int n;
        cout<<"输入集合的元素个数为"<<endl;
        cin>>n;
	    head=nn=NULL;
	    list *current;
	    list *pend;
	    int i=1;
  	    while(i<=n)
	{
		current=new list;
		cin>>current->first;
		int tt=current->first;
		for(list*w=head;w!=NULL;w=w->next)
		if(w->first==current->first)
		   exit(1);
		if(head==NULL)
			head=current;
		else
		pend->next=current;
		pend=current;
        pend->next=NULL;
        i++;   
	}
	    return (head);
}
void put(list *dd)
{
       cout<<"{";
       cout<<dd->first<<" ";
	   list *h;
	   h=dd->next;
	   while(h!=NULL)
	{
	   cout<<h->first<<" ";
	   h=h->next;
	}
	   cout<<"}"<<endl;
}
list* B(list *dd,list *bb)
{
	  int k=0;
	  list *m;
	  list *nn;
	  list *tt=dd;
	  for(bb;bb!=NULL;bb=bb->next)
	{ 
	   k=bb->first;
	   for(nn=dd;nn!=NULL;nn=nn->next)
	  {
		if(k==nn->first)
			break;
			if(nn->next==NULL)                              
			{
		   m=new list;
		   m->first=k;
		   m->next=nn->next;
		   nn->next=m;
			}	 
	  }
	}

	return  tt;
}
      list*J(list *ss,list*bb)
{     
	  int c=0;
      list *head4=NULL;
      list *pend;
      list *t;
      for(ss;ss!=NULL;ss=ss->next)
	{  
         c=ss->first;
		for(list *rr=bb;rr!=NULL;rr=rr->next)
		  if(c==rr->first)
		   { 
               t=new list;
			   t->first=c;
			   if(head4==NULL)
			   head4=t;
			   else pend->next=t;
			   pend=t;
			   pend->next=NULL;	   
		   }	      
	}
         return head4;
}
void main()
{
         list *headA;
         list *headB;
         list *a1=setup(headA);
         put(a1);
	     list *a2=setup(headB);
         put(a2);
         list *N=J(a1,a2);
         cout<<"交集为"<<"AnB=";
         put(N);
	     list *U=B(a1,a2);
         cout<<"并集为"<<"AUB=";
         put(U);
}

⌨️ 快捷键说明

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