数据结构:顺序表的使用

帮助学生熟练掌握顺序表的建立及基本操作
问题描述:设计一个顺序表并实现对其进行基本操作。
基本要求:建立一个顺序表:
(1)输入数据;
(2)实现数据的插入、删除、搜索、输出等基本操作;
(3)实现集合的并、交和两个有序顺序表的合并。

#include <stdlib.h>
#include <iostream>
using namespace std;
const int Size=100;
template <class T>
class SeqList{
protected:
    int maxSize;  //表的最大空间
    int last;    //元素最后下标
    T *data;   //数组
    void reSize(int size);    //改变数组的空间大小
public:
    SeqList(int sz=Size);   //构造函数
    SeqList(const SeqList<T> &S);   //复制构造函数
    SeqList<T> &operator=(SeqList<T> &S);  //赋值函数
    ~SeqList() {delete []data;}      //析构函数
    int Size() const  {return maxSize;}    //表的最大容量
    int Length() const {return (last+1);}   //计算表的长度
    bool isEmpty() {return (last==-1)? true:false;}  //判断表空
    bool isFull()  {return (maxSize==last+1)? true:false;}   //判断表满
    int Search1(T &x)const;      //搜索指定值,返回表项值
    int Search(T &x)const;      //搜索指定值,返回表项值
    int Locate(int i)const;     //定位第i个表项
    bool getData(int i,T &x)const;  //取出第i个表项
    bool setData(int i,T &x);   //修改第i个表项
    bool Insert(int i,T &x);      //在第i个表项后插入值
    bool Remove(int i);      //删除第i个表项
    void Sort();     //排序
    void Input();    //输入
    void Output();    //输出
    void Use(int size) {reSize(size);}    //调用reSize函数
    void Union(SeqList <T>  L);      //实现两个集合的并集
    void Intersection(SeqList<T> L);     //实现两个集合的交集
    void Union1(SeqList <T> L);    //实现有序表的合并
    void delete1(T &x);
};

template <class T>
SeqList<T>::SeqList(int sz){
    if(sz>0){
        maxSize=sz;
        last=-1;
        data=new T[sz];
        if(data==NULL)  {
            cout<<"存储分配错误!"<<endl;
            exit(1);
        }
    }
}

template <class T>
SeqList<T>::SeqList(const SeqList<T> &S)  {
    maxSize=S.Size();
    last=S.Length()-1;

    T value;
    data=new T[maxSize];
    if(data==NULL) {
       cout<<"存储分配错误!"<<endl;
            exit(1);
    }
    for(int i=1;i<=last+1;i++) {
        S.getData(i,value);
        data[i-1]=value;
    }
}

template <class T>
SeqList<T> &SeqList<T>::operator=(SeqList<T> &S) {
     maxSize=S.Size();
     last=S.Length()-1;
     T value;
     data=new T[maxSize];
     if(data==NULL)  {
        cout<<"存储分配错误"<<endl;
        exit(1);
     }
     for(int i=1;i<=last+1;i++) {
        S.getData(i,value);
        data[i-1]=value;
     }
     return (*this);
}

template <class T>
int SeqList<T>::Search1(T&x)const{
    int i;
    for(i=0;i<=last;i++)  {
        if(data[i]==x) {
           cout<<x<<" is located at index of ";
           cout<<i+1<<endl;
           return i+1;
        }
    }
    cout<<x<<" is not found"<<endl;
    return -1;
}

template <class T>
int SeqList<T>::Search(T&x)const{
    int i;
    for(i=0;i<=last;i++)  {
        if(data[i]==x) {
           cout<<i+1<<endl;
           return i+1;
        }
    }
    return -1;
}

template <class T>
int SeqList<T>::Locate(int i)const {
   if(i>=1&&i<=last+1)
        return i;
   return -1;
}

template <class T>
void SeqList<T>::delete1(T &x)   {
     int j;
     for(int i=0;i<=last;i++)  {
         if(data[i]==x)  {
            data[i]=data[i+1];
            j=i+1;
            break;
        }
     }
     for(int z=j;z<=last;z++)  {
        data[z]=data[z+1];
     }
     last--;
}

template <class T>
bool SeqList<T>::getData(int i,T &x)const {
    if(i>=1&&i<=last+1) {
        x=data[i-1];
        return true;
    }
    return false;
}

template <class T>
bool SeqList<T>::setData(int i,T &x){
     if(i>=1&&i<=last+1) {
        data[i-1]=x;
        return true;
    }
    return false;
}

template <class T>
bool SeqList<T>::Insert(int i,T &x){
    if(last+1==maxSize)  return false;
    if(i<0||i>last+1) return false;
    int j;
    for(j=last;j>=i;j--)  {
        data[j+1]=data[j];
    }
    data[i]=x;
    last++;
    return true;
}

template <class T>
bool SeqList<T>::Remove(int i){
    if(last==-1)  return false;
    if(i<1||i>last+1)  return false;
    for(int j=i-1;j<last;j++) {
        data[j]=data[j+1];
    }
    last--;
    return true;
}

template <class T>
void SeqList<T>::Input() {
cout<<"请输入需要存储的元素个数:";
cout<<"输入存进顺序表的数据:"<<endl;
    while(1)   {
        int x;
        cin>>x;
        last=x-1;
        if(last>=0&&last<=maxSize-1)
            break;
    }
    for(int j=0;j<=last;j++)  {
        cin>>data[j];
    }
}

template <class T>
void SeqList<T>::Output()  {
     for(int j=0;j<=last;j++)  {
        cout<<" "<<data[j];
     }
     cout<<endl;
}

template <class T>
void SeqList<T>::Sort()   {
     T value;
     for(int i=1;i<=last;i++)  {
        for(int j=0;j<=last-i;j++) {
            if(data[j]>data[j+1])
            {
                value=data[j];
                data[j]=data[j+1];
                data[j+1]=value;
            }
        }
     }
}

template <class T>
void SeqList<T>::reSize(int size)  {
    if(size<=0)  cout<<"无效的空间大小"<<endl;
    if(size>maxSize)  {
       T *newarray=new T[size];
       if(newarray==NULL)  {
        cout<<"存储分配错误!"<<endl;
        exit(1);
       }
       int j=last+1;
       T *a=data;
       T *b=newarray;
       while(j--)   {
          * b++=* a++;
       }
       delete []data;
       data=newarray;
       maxSize=size;
    }
}

template <class T>
void SeqList<T>::Union(SeqList<T> L){
    int m=L.Length();
    int n=this->Length();
    int flag;
    T x;
    for(int i=1;i<=n;i++)    {
        this->getData(i,x);
        flag=L.Search(x);
        if(flag==-1)  {
            L.Insert(m,x);
            m++;
        }
    }
    cout<<"A union B is";
    L.Output();
}

template<class T>
void SeqList<T>::Intersection(SeqList <T> L)  {
     int m=L.Length();
     int flag;
     T x;
     int i=1;
     while(i<=m)   {
        L.getData(i,x);
        flag=this->Search(x);
        if(flag==-1)  {
            L.Remove(i);
            m--;
        }
        else i++;
     }
    cout<<"A cross B is";
    L.Output();
}

template <class T>
void SeqList<T>::Union1(SeqList<T> L){
    this->Sort();
    L.Sort();
    int m=this->Length();
    int n=L.Length();
    int flag;
    T x;
    for(int i=1;i<=n;i++)    {
        L.getData(i,x);
        flag=this->Search(x);
        if(flag==-1)  {
            this->Insert(m,x);
            m++;
        }
    }
    cout<<"A union B in sequence is";
    this->Output();
}
int main()
{
    SeqList<int> com(20);
    com.Input();//输入顺序表元素 
    cout << "A is created as:";
    com.Output();//输出确定的顺序表元素 
/*
    int x,y;
    cin>>x>>y;
    com.Insert(x-1,y);//在x位置插入 Y元素,即在x-1表项位置
    cout<<"After Inserted A is";
    com.Output();//输出插入后的结果
    int z;
    cin>>z;
    com.delete1(z);//删除z元素
    cout<<"After deleted A is";
    com.Output();//输出删除后的结果
    int q;
    cin>>q;
    com.Search1(q);//查找元素
    SeqList<int> con(20);//定义第二个顺序表
    con.Input();
    com.Intersection(con);
    com.Union(con);
    com.Union1(con);//合并两个顺序表
*/
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值