IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 大二上集合的运算,交集、并集和差集 -> 正文阅读

[数据结构与算法]大二上集合的运算,交集、并集和差集

适用于河南大学软件学院并集、交集和差集的实验1(有错别怪我)

在实验初我有两种想法,一是使用数组好像很方便,二是使用链表;因为链表我还不是很熟悉所以我选择了链表(手动狗头)。

实验过程就是利用一个数和一个链表进行数据对比,根据集合的定义得出我们的结果,但是出现了一个小问题

由于链表并不是很熟悉没有实现链表中查找出重复数据直接重复输入而是利用人工进行下一步调出菜单选择栏时手动输入5结束程序(我觉得问题不大后期可以改进,但是我想一想好像还要加不少行代码暂时放下了后面假期有空再改进)

#include<stdio.h>      
#include<iostream>       
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h> 
using namespace std;
typedef struct node1    
{
	int data;
	struct node1* next; 
}node;  
void menu(int *ch); 
void xunhuan(int *tingzhi,int* shu,node *head1,node *head2);
node *create(void); 
void chachong(int *tingzhi,node *head);
void jiaoji(node *head1,node *head2);
void bingji(node *head1,node *head2);
void chaji(node *head1,node *head2);
void output(node* head);
int main(void)
{  

    int shu;
    int *tingzhi;
    tingzhi=0;
    node *head1,*head2;
	menu(&shu); 
	xunhuan(tingzhi,&shu,head1,head2);
		
}
void  menu(int* shu)
{
	char a01[]="(1)输入两个数组开始操作(感觉多此一步)"; 
	char a02[]="(2)求交集"; 
	char a03[]="(3)求并集"; 
	char a04[]="(4)求差集"; 
	char a05[]="(5)退出";
	printf("%-50s",a01); 
	printf("%-50s\n",a02);
	printf("%-50s",a03);
	printf("%-50s\n",a04);
	printf("%-50s\n",a05);	
	cout<<"请输入你的选择:";
	scanf("%d",shu);
//	cout<<"您输入的数是"<<*shu<<endl;作为过程中的一个检测 
}
void xunhuan(int *tingzhi,int* shu,node *head1,node *head2)
{
	while(1)
	{	
		if(*shu==1){
			    cout << "请输入数组一(输入负数时停止输入)" << endl;
            	head1=create();
	            chachong(tingzhi,head1);
	            if(*tingzhi==1)
	            {
	            	break;
				}
	            cout << "请输入数组二(输入负数时停止输入)" << endl;
            	head2=create();
	            chachong(tingzhi,head2);
	            if(*tingzhi==1)
	            {
	            	break;
				}
	            cout<<"请重新选择功能:";
				cin>>*shu; 
				xunhuan(tingzhi,shu,head1,head2);
		}else if(*shu==2){
			jiaoji(head1,head2);
		        cout<<"请重新选择功能:";
				cin>>*shu; 
				xunhuan(tingzhi,shu,head1,head2);}
		else if(*shu==3){
			 bingji(head1,head2);
		        cout<<"请重新选择功能:";
				cin>>*shu; 
				xunhuan(tingzhi,shu,head1,head2);
		}else if(*shu==4){
			chaji(head1,head2);
		        cout<<"请重新选择功能:";
				cin>>*shu; 
				xunhuan(tingzhi,shu,head1,head2);
		}else if(*shu==5){
			break; 
		} 
	  }
	
}
node *create(void)
{
	int x;
	node *head,*pre,*temp;
	head = pre = new node;
	while (scanf("%d", &x) && x > 0)
	{
		temp = new node;    
		temp->data = x;
		temp->next = NULL;
		pre->next = temp;
		pre = temp;
	}
	return head;
}
void chachong(int *tingzhi,node *head)
{
	node *temp=head->next;
	int a=temp->data;
	int zengliang=1;//用于帮助a的值向后流动 
	while(temp->next!=NULL)//最后一个数字没有查重的必要 
	{
		while(temp->next!=NULL) 
       {
      	temp=temp->next;//记住这个位置,准确记住这个链表的向后传递的位置 !! 
    	if(a==temp->data)
    	{
    		printf("您输入的第%d个数和后面的数发生了重复(不想改了)",zengliang);
    		*tingzhi=1;
			break;
		}
   	   }
		int j;
	    temp=head->next;
		for(j=0;j<zengliang;j++)
		{
		    temp=temp->next; 	
//			a=temp->data;这个东西应该在外边 
			  } 
	    a=temp->data; 
	    zengliang++;		   	
	}
}
void jiaoji(node *head1,node *head2)
{
	node *jiao,*pre,*tempjiao;
	jiao = pre = new node;	
	node *temp1=head1->next;
	node *temp2=head2->next;
	while(temp1!=NULL)//请注意观察 
	{
		while(temp2!=NULL) 
       {
    	if(temp1->data==temp2->data)
    	{
    	 tempjiao = new node;    
	     tempjiao->data = temp1->data;
		 tempjiao->next = NULL;
		 pre->next = tempjiao;
		 pre = tempjiao;
		}
		temp2=temp2->next;
   	   }
	    temp1=temp1->next;	   //这个一定要注意!要把temp2再次加入到循环里
	    temp2=head2->next;
	}
	cout<<"交集结果是:"<<endl;
	output(jiao);		 
}
void bingji(node *head1,node *head2)
{                             //从这到下面好多就是copy的jiao函数 
	node *jiao,*pre,*tempjiao;
	jiao = pre = new node;	
	node *temp1=head1->next;
	node *temp2=head2->next;
	while(temp1!=NULL)//请注意观察 
	{
		while(temp2!=NULL) 
       {
    	if(temp1->data==temp2->data)
    	{
    	 tempjiao = new node;    
	     tempjiao->data = temp1->data;
		 tempjiao->next = NULL;
		 pre->next = tempjiao;
		 pre = tempjiao;
		}
		temp2=temp2->next;
   	   }
	    temp1=temp1->next;	   //这个一定要注意!要把temp2再次加入到循环里
	    temp2=head2->next;}
       output(jiao);                           //copy结束开始干活!笑死这不就是魔改 !!!
	node *bing1,*pre1,*tempbing1;
	bing1 = pre1 = new node;	
	temp1=head1->next;
	tempjiao=jiao->next;
	while(temp1!=NULL)//请注意观察 
	{
		while(tempjiao!=NULL) 
       {
    	if(temp1->data==tempjiao->data)   //这个判断有点难 
    	{
    	   break;
		}
		tempjiao=tempjiao->next;
   	   }
   	   if(tempjiao==NULL)
   	   {
   	   	 
   	   	 tempbing1 = new node;    
	     tempbing1->data = temp1->data;
		 tempbing1->next = NULL;
		 pre1->next = tempbing1;
		 pre1 = tempbing1;
		  }
	    tempjiao=jiao->next;
	    temp1=temp1->next;
	}           
	output(bing1);    //开始继续复制  
	node *bing2,*pre2,*tempbing2;
	bing2 = pre2 = new node;	
	temp2=head2->next;
	tempjiao=jiao->next;
	while(temp2!=NULL)//请注意观察 
	{
		while(tempjiao!=NULL) 
       {
    	if(temp2->data==tempjiao->data)   //这个判断有点难 
    	{
    	   break;
		}
		tempjiao=tempjiao->next;
   	   }
   	   if(tempjiao==NULL)
   	   {
   	   	 
   	   	 tempbing2 = new node;    
	     tempbing2->data = temp2->data;
		 tempbing2->next = NULL;
		 pre2->next = tempbing2;
		 pre2 = tempbing2;
		  }
	    tempjiao=jiao->next;
	    temp2=temp2->next;
	}           
	output(bing2);            
	    //核心代码结束 
}
void chaji(node *head1,node *head2)
{
	cout<<"head1减head2的差集是:";
	node *jiao,*pre,*tempjiao;
	jiao = pre = new node;	
	node *temp1=head1->next;
	node *temp2=head2->next;
	while(temp1!=NULL)//请注意观察 
	{
		while(temp2!=NULL) 
       {
    	if(temp1->data==temp2->data)
    	{
    	 tempjiao = new node;    
	     tempjiao->data = temp1->data;
		 tempjiao->next = NULL;
		 pre->next = tempjiao;
		 pre = tempjiao;
		}
		temp2=temp2->next;
   	   }
	    temp1=temp1->next;	   //这个一定要注意!要把temp2再次加入到循环里
	    temp2=head2->next;}
	    node *bing1,*pre1,*tempbing1;
	bing1 = pre1 = new node;	
	temp1=head1->next;
	tempjiao=jiao->next;
	while(temp1!=NULL)//请注意观察 
	{
		while(tempjiao!=NULL) 
       {
    	if(temp1->data==tempjiao->data)   //这个判断有点难 
    	{
    	   break;
		}
		tempjiao=tempjiao->next;
   	   }
   	   if(tempjiao==NULL)
   	   {
   	   	 
   	   	 tempbing1 = new node;    
	     tempbing1->data = temp1->data;
		 tempbing1->next = NULL;
		 pre1->next = tempbing1;
		 pre1 = tempbing1;
		  }
	    tempjiao=jiao->next;
	    temp1=temp1->next;
	}           
	output(bing1);    //开始继续复制  
	cout<<"head2减head1的差集是:";
		node *bing2,*pre2,*tempbing2;
	bing2 = pre2 = new node;	
	temp2=head2->next;
	tempjiao=jiao->next;
	while(temp2!=NULL)//请注意观察 
	{
		while(tempjiao!=NULL) 
       {
    	if(temp2->data==tempjiao->data)   //这个判断有点难 
    	{
    	   break;
		}
		tempjiao=tempjiao->next;
   	   }
   	   if(tempjiao==NULL)
   	   {
   	   	 
   	   	 tempbing2 = new node;    
	     tempbing2->data = temp2->data;
		 tempbing2->next = NULL;
		 pre2->next = tempbing2;
		 pre2 = tempbing2;
		  }
	    tempjiao=jiao->next;
	    temp2=temp2->next;
	}           
	output(bing2);            
	    //核心代码结束 
}
void output(node* head)
{           
	node *temp = head->next;
	while (temp != NULL) 
	{
		cout<<temp->data <<"   ";
		temp = temp->next;
	}
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-19 08:13:59  更:2021-09-19 08:14:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 13:21:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码