【考研----数据结构C++】顺序表实现A∪B,A∩B,A-B

#include <iostream>
#include<ctime>
using namespace std;

#define MAXLEN 100
/**
练习5.1
*/



typedef int elementType;
//定义一个顺序表
typedef struct sllLast
{
    elementType data[MAXLEN];
    int last;
} seqList;

//初始化
void initList(seqList &S)
{
    S.last=-1;
}
//求长度
int listLength(seqList S)
{
    return S.last+1;
}
//按序号取元素
//
bool getElement(seqList S,int i,elementType &x)  //顺序表  序号  存值
{
    if(i<1||i>S.last+1)
    {
        return false;
    }
    else
    {
        x=S.data[i-1];
        return true;
    }

}
//查找元素x,成功:返回元素编号,失败返回0
int listLocate(seqList S,elementType x)
{
    for(int i=0; i<=S.last; i++)
    {
        if(x==S.data[i])
        {
            return i+1;
        }
    }
    return 0;
}
//插入元素
int listInsert(seqList &S,elementType x,int i) //顺序表,插入的值,序号
{
    if(i<1||i>S.last+2)
    {
        return 1; //插入的位置超出范围
    }
    if(S.last>MAXLEN-1)
    {
        //顺序表满了
        return 0;
    }
    for(int j=S.last; j>=i-1; j--)
    {
        S.data[j+1]=S.data[j];
    }
    S.data[i-1]=x;
    S.last++;
    return 2;
}
//删除元素,(删除序号为i的元素)
int listDelete(seqList &S,int i)
{
    if(S.last==-1)
    {
        return -1; //空表
    }
    else if(i<1||i>S.last+1)
    {
        //插入位置不对
        return -1;
    }
    else
    {
        for(int j=i; j<=S.last; j++)
        {
            S.data[j-1]=S.data[j+1];
        }
        S.last--;
        return 2;
    }
}
//打印表中元素
void printList(seqList S)
{
    for(int i=0; i<=S.last; i++)
    {
        cout<<S.data[i]<<"\t";

    }
    cout<<endl;
}

//交互输入数据元素--特殊输入结束

void listInput(seqList &S)
{

    if(S.last>=0)
    {
        cout<<"顺序表已经存在,请先初始化,在输入元素"<<endl;
        return;
    }
    elementType x;
    cout<<"请输入数据元素(整数 -9999退出)"<<endl;
    cout<<"x=";
    cin>>x;
    while(x!=-9999)
    {
        S.last++;
        S.data[S.last]=x;
        cout<<"x=";
        cin>>x;
    }
}

//随机数创建顺序表
void rndCList(seqList &S)
{
    int i;
    int n,m;
    S.last=-1;
    cout<<"请输入要产生的随机数个数,n=";
    cin>>n;
    if(n>MAXLEN-1)
    {
        cout<<"超出了查找表长度,创建失败"<<endl;
        return ;
    }
    cout<<"请输入控制随机数大小参数:";
    cin>>m;
    srand((unsigned)time(NULL));
    for(int i=0; i<n; i++)
    {
        S.data[i]=rand()%m;
    }
    S.last=n-1;
    cout<<endl;
}


/**

5.2求A∪B,A∩B,A-B

*/

//A∩B
void interSet(seqList A,seqList B,seqList &C)
{
    int j=0;
    for(int i=0; i<=A.last; i++)
    {
        if(listLocate(B,A.data[i])!=0)
        {
            listInsert(C,A.data[i],j+1);
        }
    }
}

//A∪B
void mergeSet(seqList A,seqList B,seqList &C)
{
    int j=0;
    for(int i=0; i<=A.last; i++)
    {
        listInsert(C,A.data[i],j+1);
    }
    for(int i=0; i<=1
            B.last; i++)
    {
        if(listLocate(A,B.data[i])==0)
        {
            //B的元素不在A中
            listInsert(C,B.data[i],j+1);
        }
    }
}
//A-B
void differenceSet(seqList A,seqList B,seqList &C)
{
    int j=0;
    for(int i=0; i<=A.last; i++)
    {

        if(listLocate(B,A.data[i])==0)
        {
            listInsert(C,A.data[i],j+1);

        }
    }
}
int main()
{
    /*
    seqList S;
    initList(S);
    // rndCList(S);
    // printList(S);
    cout<<S.last<<endl;
    listInput(S);
    printList(S);
    */
    seqList A,B,C,D;
    initList(A);
    initList(B);
    initList(C);
    initList(D);
    cout<<"随机生成顺序表A:"<<endl;
    listInput(A);
    printList(A);
    cout<<"随机生成顺序表B:"<<endl;
    listInput(B);
    printList(B);
    cout<<"A∪B的结果为:"<<endl;
    mergeSet(A,B,C);
    printList(C);
    cout<<"A∩B的结果为:"<<endl;
    interSet(A,B,D);
    printList(D);
    cout<<"A-B的结果为:"<<endl;
    differenceSet(A,B,C);
    printList(C);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

William_Tao(攻城狮)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值