顺序存储结构实现线性表ADT

顺序存储结构实现线性表ADT


线性表是最常见和常用的ADT。假设线性表的元素为整数,请基于顺序存储结构实现线性表ADT。
基本功能包括:
(1)建立线性表;
输入有两行,第一行是一个整数n,线性表的长度; 第二行是n和数据元素
(2)插入:
输入两个整数,即元素插入的位置和元素值
(3)删除:
输入一个整数,即要删除的元素
(4)搜索:
输入一个整数,即搜索元素的值
(5)输出:
输出线性表的各个元素,空格分开。
(6)集合的并运算:
输入创建第二个集合(线性表),完成并运算
(7)集合的交运算:
输入创建第二个集合(线性表),完成交运算
(8)合并两个有序线性表:
两个有序线性表,合并后仍然有序
测试数据:
5 //线性表A的长度
1 3 5 7 9 //线性表A的数据
2 10 //表示在第2个位置插入10
10 //表示删除值=10的数据元素
9 //查找元素9
22 / /查找元素22
6 //线性表B的长度
1 2 3 4 5 6
例如:
输入
Result
5
1 3 5 7 9
2 10
10
9
22
6
1 2 3 4 5 6
A is created as: 1 3 5 7 9
After inserted A is 1 10 3 5 7 9
After deleted A is 1 3 5 7 9
9 is located at index of 5
22 is not found
B is created as: 1 2 3 4 5 6
A cross B is 1 3 5
A union B is 1 3 5 7 9 2 4 6
A union B in sequence is 1 2 3 4 5 6 7 9

方法一:

#include <iostream>
#include <cstdlib>

using namespace std;

typedef int ElemType;
typedef struct
{
ElemType data[105];
int maxSize;
int last;
}PList;
void Create(PList&l,int n)
{
    l.last=-1;
    l.maxSize=105;
    for(int i=0;i<n;i++)
    {
        cin>>l.data[i];
        l.last++;
    }

}
void Input(PList &l)
{
    for(int i=0;i<=l.last;i++)
    {
        cout<<" "<<l.data[i];
    }
    cout<<endl;
}
void Insert(PList & l,int pos,int data)
{
    if(l.last==l.maxSize-1)
    {
        cerr<<"error"<<endl;
        exit(1);
    }
    if(pos<1||pos-1>l.last+1)
    {
        cerr<<"error"<<endl;
        exit(1);
    }

    for(int i=l.last;i>=pos-1;i--)
    {
        l.data[i+1]=l.data[i];
    }
    l.data[pos-1]=data;
    l.last++;
}
int  Search(PList &l,int data)
{
    int i=0;
    while(i<=l.last&&l.data[i]!=data)
    {
        i++;
    }
    if(i>l.last)
    {
        return -1;
    }
    else
    {
        return i+1;
    }
}
int Delete(PList&l,int data)
{
    int pos=Search(l,data);
    if(pos>=0)
    {
    for(int i=pos;i<=l.last;i++)
    {
        l.data[i-1]=l.data[i];
    }
    l.last--;
    return 1;
    }
    return 0;

}

PList cross(PList &A,PList &B)
{
    PList C;
    Create(C,0);
    int count=1;
  for(int i=0;i<=A.last;i++)
  {
      //cout<<"for"<<endl;
      int temp=A.data[i];
      if(Search(B,temp)>=0)
      {
          Insert(C,count,temp);
          count++;
      }
  }
  return C;
}

PList Union(PList&A,PList&B)
{
   // cout<<"enter union"<<endl;
    PList C;
    Create(C,0);
    //cout<<"created"<<endl;
    int count=1;
    int temp;
    for(int i=0;i<=A.last;i++)
    {
        temp=A.data[i];
        //cout<<temp;
        if(Search(C,temp)<=0)
        {
            Insert(C,count,temp);
            count++;
        }
    }

    for(int i=0;i<=B.last;i++)
    {
        temp=B.data[i];
         if(Search(C,temp)<=0)
        {
            Insert(C,count,temp);
            count++;
        }
    }//Input(C);
    return C;

}
PList SortUnion(PList &A,PList &B)
{
    int indexA=0;
    int indexB=0;
    PList C;
    Create(C,0);
    int count=1;
    int dataA,dataB;
    while(indexA<=A.last&&indexB<=B.last)
    {
        dataA=A.data[indexA];
        dataB=B.data[indexB];
        if(dataA==dataB)
        {
            Insert(C,count,dataA);
            count++;
            indexA++;
            indexB++;
        }
        else if(dataA>dataB)
        {
            Insert(C,count,dataB);
            count++;
            indexB++;
        }
        else
        {
            Insert(C,count,dataA);
            count++;
            indexA++;
        }
    }
if(indexA<=A.last)
{
   // cout<<"index"<<indexA<<endl;
    for(int i=indexA;i<=A.last;i++)
    {
        Insert(C,count,A.data[i]);
       // cout<<A.data[i];
        count++;
    }
}
if(indexB<=B.last)
{
     for(int i=indexB;i<=B.last;i++)
    {
        Insert(C,count,B.data[i]);
        count++;
    }
}
    return C;

}
int main()
{
    int n;
    int data;
    cin>>n;
    PList A;
    Create(A,n);
    cout<<"A is created as:";
    Input(A);
    cin>>n>>data;
    Insert(A,n,data);
    cout<<"After inserted A is";
    Input(A);
    cin>>data;
    Delete(A,data);
    cout<<"After deleted A is";
    Input(A);
    cin>>data;
    int temp=Search(A,data);
    if(temp>=0)
    {
        cout<<data<<" is located at index of "<<temp;
    }
    else
    {
        cout<<data<<" is not found";
    }
    cout<<endl;
    cin>>data;
    temp=Search(A,data);
      if(temp>=0)
    {
        cout<<data<<" is located at index of "<<temp<<endl;
    }
    else
    {
        cout<<data<<" is not found"<<endl;
    }
    PList B;
    cin>>n;
    Create(B,n);
     cout<<"B is created as:";
     Input(B);
    PList C;
    C=cross(A,B);
    cout<<"A cross B is";
    Input(C);
    C=Union(A,B);
    cout<<"A union B is";
    Input(C);
    C=SortUnion(A,B);
    cout<<"A union B in sequence is";
    Input(C);
    return 0;
}

方法二

#include <iostream>
#include <stdlib.h>

using namespace std;

typedef int T;
typedef int ElemType;

class SeqList
{
public:
    T *data;
    int MaxSize;
    int last;
    SeqList(int sz);
    ~SeqList(){
        delete []data;
    }
    int Length() const{  //返回元素的个数
        return (last + 1);
    }
    int Search(T &x) const;  //返回元素x在表中的位置
    void Insert(int i, T &x);  //在第i个位置插入元素x
    void Delete(int i);  //删除第i元素
    void Delete1(SeqList &s,T &x);
    int IsEmpty(){  //表空否
        return last == -1;
    }
    int IsFull(){  //判断是否满
        return last == MaxSize-1;
    }
    T GetData(int i){  //获得第i个元素
        return data[i-1];
    }
    //T GetPrior(T &x);  //取x前驱元素
    //T GetNext(T &x);  //取x的后继元素
    void PrintList();  //输出线性表
    SeqList Cross(SeqList &sb);
    SeqList Union(SeqList &sb);
    SeqList SeUnion(SeqList &sb);
};
SeqList SeqList::SeUnion(SeqList &sb)
{
    int m = MaxSize+sb.MaxSize+5;
    SeqList sc(m);
    int d1,d2;
    int i=0;
    int j=0;
    while((i<=last)&&(j<=sb.last))
    {
        d1 = data[i];
        d2 = sb.data[j];
        if(d1 == d2)
        {
            sc.Insert(sc.last+1,d1);
            i++;
            j++;
        }else if(d1 < d2){
            sc.Insert(sc.last+1,d1);
            i++;
        }else{
            sc.Insert(sc.last+1,d2);
            j++;
        }
    }
    if(i<=last){
        for(int q=i; q<=last; q++){
            sc.Insert(sc.last+1,data[q]);
        }
    }
    if(j<=sb.last){
        for(int q=j; q<=sb.last; q++){
            sc.Insert(sc.last+1,data[q]);
        }
    }
    return sc;
}


SeqList SeqList::Union(SeqList &sb)
{
    int m = MaxSize+sb.MaxSize+5;
    SeqList sc(m);
    for(int i=0; i<=last; i++){
        sc.Insert(sc.last+1,data[i]);
    }
    for(int i=0; i<=sb.last; i++)
    {
        if(sc.Search(sb.data[i])==-1){
            sc.Insert(sc.last+1,sb.data[i]);
        }
    }
    return sc;
}

SeqList SeqList::Cross(SeqList &sb){
    int m = min(MaxSize,sb.MaxSize);
    SeqList sc(m);
    for(int i=0; i<=last; i++){
        if(sb.Search(data[i])!=-1){
            sc.Insert(sc.last+1,data[i]);
        }
    }
    //sc.PrintList();
    return sc;
}

void SeqList::Delete1(SeqList &s,T &x){
    if(s.Search(x)==-1){
        cout<<x<<" is not found"<<endl;
    }else{
        s.Delete(s.Search(x)-1);
    }
}
//输出线性表
void SeqList::PrintList()
{
    int i=-1;
    while(++i < last){
        cout<<data[i]<<" ";
    }
    cout<<data[last]<<endl;
}
//构造函数,指定sz定义数组的长度
SeqList::SeqList(int sz)
{
    if(sz>0)
    {
        data = new T[sz];  //分配连续空间
        if(data != NULL)
        {
            MaxSize = sz*2;
            last = -1;
        }else{
            cerr<<"存储分配错误!"<<endl;
            exit(1);
        }
    }
}
//顺序查找x
int SeqList::Search(T &x)const
{
    int i = 0;
    if(last == -1)
        cerr<<"所查询的表为空"<<endl;
    while(i <= last && data[i]!=x)
        i++;
    if(i>last)
        return -1;
    else
        return i+1;
}
//插入元素到第i个位置
void SeqList::Insert(int i, T &x)
{
    if(last == MaxSize-1)
    {
        cerr<<"顺序表已满,无法插入!"<<endl;
        exit(0);
    }
    if(i<0||i>last+1)
    {
        cerr<<"参数i越界出错!"<<endl;
        exit(1);
    }
    for(int j=last; j>=i; j--){
        data[j+1] = data[j];
    }
    data[i] = x;
    last++;
}
//删除第i个元素
void SeqList::Delete(int i){
    if(i>=0){
        for(int j=i; j<last;j++){
            data[j] = data[j+1];
        }
        last--;
    }else
        cerr<<"下标越界!"<<endl;
}

int main()
{
    int n;
    int a0;
    cin>>n;
    SeqList sl(n);
    while(n--){
        cin>>a0;
        sl.Insert(sl.last+1,a0);
    }
    cout<<"A is created as: ";
    sl.PrintList();
    int a1,a2;
    cin>>a1>>a2;
    sl.Insert(a1-1,a2);
    cout<<"After inserted A is ";
    sl.PrintList();
    int a3;
    cin>>a3;
    sl.Delete1(sl,a3);
    cout<<"After deleted A is ";
    sl.PrintList();
    int a4;
    cin>>a4;
    if(sl.Search(a4)==-1){
        cout<<a4<<" is not found"<<endl;
    }else{
        cout<<a4<<" is located at index of "<<sl.Search(a4)<<endl;
    }
    int a5;
    cin>>a5;
    if(sl.Search(a5)==-1){
        cout<<a5<<" is not found"<<endl;
    }else{
        cout<<a5<<" is located at index of "<<sl.Search(a5)<<endl;
    }
    int bn,b0;
    cin>>bn;
    SeqList s2(bn);
    while(bn--){
        cin>>b0;
        s2.Insert(s2.last+1,b0);
    }
    cout<<"B is created as: ";
    s2.PrintList();
    cout<<"A cross B is ";
   // int m = min(sl.MaxSize,s2.MaxSize);
   // SeqList s3(m+5);
    sl.Cross(s2).PrintList();
    cout<<"A union B is ";
    sl.Union(s2).PrintList();
    cout<<"A union B in sequence is ";
    sl.SeUnion(s2).PrintList();
    return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值