C++实现顺序表

#include<iostream>
#include<string>
using namespace std;
const int maxsize=100;
bool error;
template <class T>
class seqlist{
public:
    seqlist(){length=0;};
    seqlist(T a[],int n);
    ~seqlist(){};
void insert(int i,T x);
int getlength(){return length;
}
T del(int i);
T get(int i);
int locate(T x);
void print();
private:
        T data[maxsize];
        int length;
};

template<class T>
seqlist<T>::seqlist(T a[],int n)
{
    int i;
    if(n>maxsize){cout<<"上溢"<<endl; error=true; }
    else for(i=0;i<n;i++) data[i]=a[i];
}
template<class T>
void seqlist <T>::insert (int i,T x)
{
    int j;
    if(length>=maxsize){cout <<"表满"<<endl ;error=true;}
    else if(i>length+1||i<1){
        cout <<"位置异常"<<endl; error=true;}
    else{
        for(j=length-1;j>=i-1;j--)
            data[j+1]=data[j];
            data[i-1]=x;
            length++;
    }
}
template<class T>
T seqlist <T>::del (int i){
    T x;int j;
    if(i>maxsize||i<1){
        cout<<"位置异常"<<endl; error=true; return 0;}
    else{
        x=data[i-1];
        for(j=i-1;j<=length-1;j++)
            data[j]=data[j+1];
        length--;
        return x;
    }
}
template<class T>
T seqlist<T>::get(int i)
{
 if(i>maxsize||i<1) {cout<<"位置异常"<<endl; error=true;return -1;}
 else return data[i-1];
}
template<class T>
int seqlist<T>::locate (T x)
{
    int i;
    for(i=0;i<length;i++)
        if(data[i]==x)
            return i+1;
        return 0;
    }
template <class T>
void seqlist<T>::print(){
    int i;
    cout<<"----------------------------------------";
    cout<<"|";
    for(i=0;i<length;i++)
        cout<<""<<data[i]<<"|";
        cout<<endl;
        cout<<"--------------------------------------";
    }
    int main(){
        cout<<"顺序表的实现(模板类)"<<endl;
 cout<<"************************************"<<endl;
 cout<<"*  1.插入                          *"<<endl;
 cout<<"*  2.按位查找  取第i个元素         *"<<endl;
 cout<<"*  3.按值查找  求x为第几个元素     *"<<endl;
 cout<<"*  4.删除                          *"<<endl;
 cout<<"*  5.输出顺序表                    *"<<endl;
 cout<<"*  6.输出表长                      *"<<endl;
 cout<<"*  7/help 输出此表                 *"<<endl;
 cout<<"*  8/exit.退出                     *"<<endl;
 cout<<"************************************"<<endl;
 
 int flag,ins_loc,x,tab,len;
 flag=0;
 seqlist<int>List;
 while (flag==0)
 {
  error=false;
  cout<<"Please input the command(input 7 to get the command list):"<<endl;
  cin>>tab;
  switch(tab)
  {
  case 1:
   {
    cout<<"Please input the insert location:"<<endl;
    cin>>ins_loc;
    cout<<"Please input the insert num:"<<endl;
    cin>>x;
    List.insert(ins_loc,x);
    break;
   }
  case 2:
   {
    cout<<"Please input the location:"<<endl;
    cin>>ins_loc;
    x=List.get(ins_loc);
    if(!error)
       cout <<"The number is:"<<x<<endl;
    break;
   }
  case 3:
   {
    cout<<"Please input the number you want to find in the list:"<<endl;
    cin>>x;
    ins_loc=List.locate(x);
    if(!error)
     cout<<"The number's location is:"<<ins_loc<<endl;
    break;
   }
  case 4:
   {
    cout<<"Please input the location you want to delete:"<<endl;
    cin>>ins_loc;
    List.del(ins_loc);
    break;
   }
  case 5:
   {
    List.print();
    break;
   }
  case 6:
   {
    if(!error)
     len=List.getlength();
     cout<<"The length is:"<<len<<endl;
    break;
   }
  case 7:
   {
              cout<<"************************************"<<endl;
                cout<<"*  1.插入                          *"<<endl;
             cout<<"*  2.按位查找  取第i个元素         *"<<endl;
             cout<<"*  3.按值查找  求x为第几个元素     *"<<endl;
              cout<<"*  4.删除                          *"<<endl;
             cout<<"*  5.输出顺序表                    *"<<endl;
             cout<<"*  6.输出表长                      *"<<endl;
             cout<<"*  7/help 输出此表                 *"<<endl;
              cout<<"*  8/exit.退出                     *"<<endl;
             cout<<"************************************"<<endl;
    break;
   }
  case 8:
   {
    flag=1;
    break;
   }
  default:
   {
    cout<<"The command is no found!"<<endl;
    break;
   }
  }
 }
 return 0;
}
    

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值