【数据结构】【单向链表】实现集合交并差等操作

题目

编写一组程序,用带头结点的有序单链表实现一个集合,放在“SetList. h"中。要求实现这个集合的结构和相关的操作。至少应包括初始化,用尾插法建立集合,查找给定元素是否在集合内,将新元素插入集合中,删除集合中指定元素,求两个集合的并、交、差、输出等操作的实现。
要求设计一个主程序,首先定义3个用有序单链表实现的集合A、B、C并初始化为空集合,其元素的数据类型为整型;再依次读入若干整数创建一个集合A和B,并检查集合上的查找、插入、删除等操作的实现,存入C中并输出它。

实现

//main.cpp
#include "SetList.h"
int main()
{
    linklist A,B,C;
    init(A);init(B);init(C);
    
	cout<<"创建A集合:"<<"\n";
	cout<<"请输入A的终止输入标志:"<<"\n";
	int end;
	cin>>end;
	build(A,end);
	
	cout<<"创建B集合:"<<"\n";
	cout<<"请输入B的终止输入标志:"<<"\n";
	int endb;
	cin>>endb;
	build(B,endb);
	
	cout<<"请选择相关操作:1-查找,2-插入,3-删除,0-结束:"<<"\n";
	int key;
	cin>>key;
	
	while(key)
	{
	switch(key)
	{
		case 1:{
			cout<<"请输入操作的对象,1-A,2-B:"<<"\n";
			int m;
			cin>>m;
			cout<<"请输入需要查找的值"<<"\n";
			int n;
			cin>>n;
			if(m==1)
			{
				if(find(A,n)==-1)
					cout<<"未找到!"<<"\n";
				else
					cout<<"位置是:"<<find(A,n)<<"\n"; 
			} 
			else
			{
				if(find(B,n)==-1)
					cout<<"未找到!"<<"\n";
				else
					cout<<"位置是:"<<find(B,n)<<"\n"; 
			} 
			break;
		}
		case 2:{
			cout<<"请输入操作的对象,1-A,2-B:"<<"\n";
			int m;
			cin>>m;
			cout<<"请输入要插入的值"<<"\n";
			int n;
			cin>>n;
			cout<<"请输入要插入的位置"<<"\n";
			int k;
			cin>>k;
			if(m==1)
				insert(A,k,n);
			else
				insert(B,k,n);
			break;
		}
		case 3:{
			cout<<"请输入操作的对象,1-A,2-B:"<<"\n";
			int m;
			cin>>m;
			cout<<"请输入要删除的位置"<<"\n";
			int k;
			cin>>k;
			if(m==1)
				remove(A,k);
			else
				remove(B,k);
			break;
		}
		default :break;
	}
	cout<<"请选择相关操作:1-查找,2-插入,3-删除,0-结束:"<<"\n";
	cin>>key;
	}
	cout<<"得到C:"<<"\n"; 
	bing(A,B,C);
	print(C);
    return 0;
}
//SetList.h
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef int type;
typedef struct node{                                        
    type data;                                          
    struct node *link;                                      
}node,*linklist;  
                                      
void init(linklist& S){ //初始化                             
    S = (node*) malloc(sizeof (node));             
    S->link = NULL;                                 
}

void build( linklist& first, type end )//尾插法构建链表 
{
    type val;  
    node *s, *rear = first;
    cout<<"请输入数据:"<<"\n";
	cin>>val;
    while ( val != end) {
        s = (node *) malloc ( sizeof (node ));
        s->data = val;  		
        rear->link = s;   rear = s;	
        cout<<"请输入数据:"<<"\n";
        cin>>val;	   
    }
    rear->link = NULL;		
} 
int find ( linklist & first, type x ) //寻找
{
	int i=1;
    node * p = first->link;        
    while (p!= NULL)  
    {if(p->data == x) return i;
	p = p->link;
    i++;
	}
    return 0;     
}


void print(linklist& first ) {	//输出链表
    first=first->link;
	while(first)
        {cout<<first->data<<"\n";
        first=first->link ;
		}
    cout<<"\n";
}
void insert( linklist& first, int i, type x ) {//插入
	if(i<1) cout<<"插入失败"<<"\n"; 
    node *newnode;
    newnode = (node *) malloc (sizeof (node));
	newnode->data = x;
    node *p = first, *pr;   int k = 0;
    while ( p != NULL && k < i-1 ) 
        { pr = p;  p = p->link;  k++; } 
	if ( p == NULL && first != NULL ) 
		p = pr;
    newnode->link = p->link;
    p->link = newnode;
    cout<<"插入成功"<<"\n"; 						
} 

bool remove(linklist& first,  int i) //删除
{
    node *p, *q;   int k;
    if ( i == 0 ) return false;    
    else 
	{			     
        p = first;  int k = 0;        
        while ( p != NULL && k < i-1 )
                { p = p->link; k++; }
		if ( p == NULL || p->link == NULL ) 
			{
            cout<< "无效的删除位置!"<<"\n";
            return false;	    
      		}
      	else {                         	    
            q = p->link;       	    
            p->link = q->link;        
      		}
        free (q);   
    }
      return true; 
}

void insertA(linklist &first , type x)//在结尾插入
{
	node *newnode;
    newnode = (node *) malloc (sizeof (node));
	newnode->data = x;
    node *p = first, *pr;   
    while ( p != NULL  ) 
        { pr = p;  p = p->link; } 
	if ( p == NULL && first != NULL ) 
		p = pr;
    newnode->link = p->link;
    p->link = newnode;
}


void bing(linklist &a, linklist &b, linklist& c)//并
{ 
	a = a->link;
	b = b->link;
	while (a)
	{
		if (!find(c,a->data))
		{
			insertA(c, a->data);
		}
		a = a->link;
	}
	while (b)
	{
		if (!find(c,b->data))
		{
			insertA(c, b->data);
		}
		b = b->link;
	}
}
  
void jiao(linklist &L1, linklist &L2, linklist& L3)//交
{
	L1 = L1->link;
	L2 = L2->link;
	while (L1)
	{
		if (find(L2,L1->data))
		{
			insertA(L3, L1->data);
		}
		L1 = L1->link;
	}
}
void cha(linklist &L1, linklist &L2, linklist& L3)//差
{
	L1 = L1->link;
	L2 = L2->link;
	while (L1)
	{
		if (!find(L2,L1->data))
			insertA(L3, L1->data);
		L1 = L1->link;
	}
}
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值