#include<iostream>
using namespace std;
const int MaxSize=10;
class SeqList
{
public:
SeqList(){length=0;}
SeqList(int a[],int n);
~SeqList(){}
void Insert(int i,int x);
int Delete(int i);
int Locate(int x);
void PrintList();
private:
int data[MaxSize];
int length;
};
SeqList::SeqList(int a[],int n)
{if(n>MaxSize) throw"²ÎÊý·Ç·¨";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void SeqList::Insert(int i,int x)
{if(length>=MaxSize) throw"ÉÏÒç";
if(i<1||i>length+1)throw"λÖ÷Ƿ¨";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
int SeqList::Delete(int i)
{if(length==0)throw"ÏÂÒç";
if(i<1||1>length)throw"λÖ÷Ƿ¨";
int x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
int SeqList::Locate(int x)
{for(int i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void SeqList::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
int r[5]={1,3,4,7,13};
SeqList L(r,5);
cout<<"Ö´ÐвåÈë²Ù×÷Ç°µÄÊý¾ÝΪ:"<<endl;
L.PrintList();
try{L.Insert(4,6);}
catch(char*s){
cout<<s<<endl;}
cout<<"Ö´ÐвåÈë²Ù×÷ºóµÄÊý¾ÝΪ;"<<endl;
L.PrintList();
cout<<"ֵΪ3µÄÔªËØλÖÃΪ:";
cout<<L.Locate(3)<<endl;
cout<<"Ö´ÐÐɾ³ýµÄµÚÒ»¸öÔªËزÙ×÷,ɾ³ýÇ°Êý¾ÝΪ:"<<endl;
L.PrintList();
try{L.Delete(1);
}
catch(char*s){cout<<s<<endl;}
cout<<"ɾ³ýºóÊý¾ÝΪ:"<<endl;
L.PrintList();
}
using namespace std;
const int MaxSize=10;
class SeqList
{
public:
SeqList(){length=0;}
SeqList(int a[],int n);
~SeqList(){}
void Insert(int i,int x);
int Delete(int i);
int Locate(int x);
void PrintList();
private:
int data[MaxSize];
int length;
};
SeqList::SeqList(int a[],int n)
{if(n>MaxSize) throw"²ÎÊý·Ç·¨";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
void SeqList::Insert(int i,int x)
{if(length>=MaxSize) throw"ÉÏÒç";
if(i<1||i>length+1)throw"λÖ÷Ƿ¨";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
int SeqList::Delete(int i)
{if(length==0)throw"ÏÂÒç";
if(i<1||1>length)throw"λÖ÷Ƿ¨";
int x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
int SeqList::Locate(int x)
{for(int i=0;i<length;i++)
if(data[i]==x)return i+1;
return 0;
}
void SeqList::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
int r[5]={1,3,4,7,13};
SeqList L(r,5);
cout<<"Ö´ÐвåÈë²Ù×÷Ç°µÄÊý¾ÝΪ:"<<endl;
L.PrintList();
try{L.Insert(4,6);}
catch(char*s){
cout<<s<<endl;}
cout<<"Ö´ÐвåÈë²Ù×÷ºóµÄÊý¾ÝΪ;"<<endl;
L.PrintList();
cout<<"ֵΪ3µÄÔªËØλÖÃΪ:";
cout<<L.Locate(3)<<endl;
cout<<"Ö´ÐÐɾ³ýµÄµÚÒ»¸öÔªËزÙ×÷,ɾ³ýÇ°Êý¾ÝΪ:"<<endl;
L.PrintList();
try{L.Delete(1);
}
catch(char*s){cout<<s<<endl;}
cout<<"ɾ³ýºóÊý¾ÝΪ:"<<endl;
L.PrintList();
}