线性表的合并之求解一般集合的并集问题(单链表)

目录

1问题描述:

2问题分析:

3代码如下:

4运行结果:


1问题描述:


        已知两个集合A和B,现要求一个新的集合A=AuB。例如,设

                A=(7,5,3,11),B=(1,2,3)

要求合并后为(7,5,3,11,1)(即遵循集合互异性的原则)

2问题分析:


可以利用两个线性表LA 和LB 分别表示集合A 和B(即线性表中的数据元素为集合中的成员), 这样只需扩大线性表 LA,将存在于 LB中而不存在于 LA 中的数据元素插入到 LA 中去。只要从LB中依次取得每个数据元素, 并依值在 LA中进行查访, 若不存在, 则插入之。
上述操作过程可用算法 2.15 来描述。具体实现时既可采用顺序形式, 也可采用链表形式。
算法2.15 线性表的合并
【算法步骤】
① 分别获取LA 表长m 和LB表长n)
②从LB中第1个数据元素开始, 循环n次执行以下操作:
● 从LB中查找第i(1≤i≤n)个数据元素赋给e;
● 在LA 中查找元素e, 如果不存在, 则将e插在表LA的最后。

主要思想就是从B里挨个取出元素,然后遍历A看是否有重复,若没有就插入A的最后即可

3代码如下:

#include<iostream>
using namespace std;
#include<stdlib.h>
#define ok -1
#define error -2
typedef int Status;
typedef int ElemType; 
typedef struct LNode
{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
Status InitList(LinkList &L,LinkList &M);
Status fuzhi(LinkList &L,int m);
void shuchu(LinkList L);
Status GetElem(LinkList L,int i,ElemType &e);
Status Length(LinkList L);
Status find1(LinkList L,ElemType e);
Status ListInsert(LinkList &L,int m,ElemType e);
int main()
{
	LinkList L,M;
	int x,m,n;
	ElemType y,e;
	x=InitList(L,M);
	if(x==error) cout<<"初始化失败";
	else         cout<<"初始化成功"<<endl;
	cout<<"请决定为线性表L赋几个元素:";
	cin>>x;
	fuzhi(L,x);
	cout<<"线性表L为:"<<endl;
	shuchu(L); 
	cout<<"请决定为线性表M赋几个元素:";
	cin>>x;
	fuzhi(M,x);
	cout<<"线性表M为:"<<endl;
	shuchu(M);
	m=Length(L);
	cout<<"m长度为:"<<m<<endl;
	n=Length(M);
	cout<<"n长度为:"<<n<<endl;
	for(int i=1;i<=n;i++)
	{
		GetElem(M,i,e);
		if(find1(L,e)!=error)
		{
			m++;
			ListInsert(L,m,e);
		}
			
		shuchu(L);
	}
	return 0;
}
Status InitList(LinkList &L,LinkList &M)
{
	L=new LNode;
	M=new LNode;
	if(L==NULL||M==NULL) return error;
	L->next=NULL;
	M->next=NULL;
	return ok;
}
Status fuzhi(LinkList &L,int m)
{
	LNode *s,*r=L;
	ElemType n;
	for(int i=1;i<=m;i++)
	{
		cout<<"请输入第"<<i<<"个元素";
		cin>>n; 
		s=new LNode;
		s->data=n;
		r->next=s;
		r=s;
	}
	r->next=NULL;
	return ok;
}
void shuchu(LinkList L)
{
	LNode *p;
	p=L->next;
	cout<<"单链表内容为:";
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
Status ListInsert(LinkList &L,int x,ElemType e)//插入 前者为位置后者是元素 
{
	LNode *p=L->next,*s;
	int j=0;
	while(p->next) 
	{
		p=p->next;
	}
	s=new LNode;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return ok;
}
Status Length(LinkList L)
{
	int i,j=0;
	LNode *p=L->next; 
	while(p!=NULL)
	{
		p=p->next;
		++j;
	}
	return j;
}
Status GetElem(LinkList L,int i,ElemType &e)
{
	LNode *p=L->next; 
	int j=0;
	while(p!=NULL)
	{
		j++;
		if(j==i) 
		{
			e=p->data;
			return ok;
		}
		p=p->next;
	}
}
Status find1(LinkList L,ElemType e)
{
	LNode *p=L->next; 
	while(p!=NULL)
	{
		if(p->data==e) return error;
		else p=p->next;
	}
	return ok;
}

#include<iostream>
using namespace std;
#include<stdlib.h>
#define ok -1
#define error -2
typedef int Status;
typedef int ElemType; 
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;
Status InitList(LinkList &L,LinkList &M);
Status fuzhi(LinkList &L,int m);
void shuchu(LinkList L);
Status GetElem(LinkList L,int i,ElemType &e);
Status Length(LinkList L);
Status find1(LinkList L,ElemType e);
Status ListInsert(LinkList &L,int m,ElemType e);
int main()
{
    LinkList L,M;
    int x,m,n;
    ElemType y,e;
    x=InitList(L,M);
    if(x==error) cout<<"初始化失败";
    else         cout<<"初始化成功"<<endl;
    cout<<"请决定为线性表L赋几个元素:";
    cin>>x;
    fuzhi(L,x);
    cout<<"线性表L为:"<<endl;
    shuchu(L); 
    cout<<"请决定为线性表M赋几个元素:";
    cin>>x;
    fuzhi(M,x);
    cout<<"线性表M为:"<<endl;
    shuchu(M);
    m=Length(L);
    cout<<"m长度为:"<<m<<endl;
    n=Length(M);
    cout<<"n长度为:"<<n<<endl;
    for(int i=1;i<=n;i++)
    {
        GetElem(M,i,e);
        if(find1(L,e)!=error)
        {
            m++;
            ListInsert(L,m,e);
        }
            
        shuchu(L);
    }
    return 0;
}
Status InitList(LinkList &L,LinkList &M)
{
    L=new LNode;
    M=new LNode;
    if(L==NULL||M==NULL) return error;
    L->next=NULL;
    M->next=NULL;
    return ok;
}
Status fuzhi(LinkList &L,int m)
{
    LNode *s,*r=L;
    ElemType n;
    for(int i=1;i<=m;i++)
    {
        cout<<"请输入第"<<i<<"个元素";
        cin>>n; 
        s=new LNode;
        s->data=n;
        r->next=s;
        r=s;
    }
    r->next=NULL;
    return ok;
}
void shuchu(LinkList L)
{
    LNode *p;
    p=L->next;
    cout<<"单链表内容为:";
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}
Status ListInsert(LinkList &L,int x,ElemType e)//插入 前者为位置后者是元素 
{
    LNode *p=L->next,*s;
    int j=0;
    while(p->next) 
    {
        p=p->next;
    }
    s=new LNode;
    s->data=e;
    s->next=p->next;
    p->next=s;
    return ok;
}
Status Length(LinkList L)
{
    int i,j=0;
    LNode *p=L->next; 
    while(p!=NULL)
    {
        p=p->next;
        ++j;
    }
    return j;
}
Status GetElem(LinkList L,int i,ElemType &e)
{
    LNode *p=L->next; 
    int j=0;
    while(p!=NULL)
    {
        j++;
        if(j==i) 
        {
            e=p->data;
            return ok;
        }
        p=p->next;
    }
}
Status find1(LinkList L,ElemType e)
{
    LNode *p=L->next; 
    while(p!=NULL)
    {
        if(p->data==e) return error;
        else p=p->next;
    }
    return ok;
}

4运行结果:

  • 43
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

杰克尼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值