データ構造順序表(C++クラス実装)


include
using namespace std;const int MaxSize = 100;template class SeqList{public:SeqList(){length=0;}SeqList(DataType a[],int n);~SeqList(){};int Length(){return length;}//DataType Get(int i);//ビット別int Locate(DataType x);//void Insert(int i,DataType x);//DataType Delete(int i);//を挿入する位置決め削除void PrintList();//void Reverse();//を印刷する逆置きvoid Cancel(int i);//特定の数void Sort();//を削除(挿入)ソートprivate:DataType data[MaxSize];int length;};
template SeqList::SeqList(DataType a[],int n){if(n>MaxSize) throw "wrong parameter";for(int i=0;idata[i]=a[i];length=n;}
template DataType SeqList::Get(int i){if(i<1 && i>length) throw "wrong Location";else return data[i-1];}
template int SeqList::Locate(DataType x){for(int i=0;iif(data[i]==x) return i+1;return 0;}
template void SeqList::Insert(int i,DataType x){if(length>=MaxSize) throw "Overflow";if(i<1 || i>length+1) throw "Location";for(int j=length;j>=i;j--)data[j]=data[j-1];data[i-1]=x;length++;}
template DataType SeqList::Delete(int i){int x;if(length==0) throw "Underflow";if(i<1 || i>length) throw "Location";x = data[i-1];for(int j=i;jdata[j-1] = data[j];length--;return x;}
template void SeqList::PrintList(){for(int i=0;icout<}
template void SeqList::Reverse() {
int j=length;
for(int i=0;i

}
template void SeqList::Sort(){
int i,j,temp;
for(i=1;i=0&&temp

}
template void SeqList::Cancel(int x){
int i,a=0,k;
int j=length-1;
for(i=0;i<=j;i++)
{
    int k=i;
    if(data[k]==x)
    {
        a=1;
        --length;
    }
    while(a==1&&k<=j)
    {
        data[k]=data[k+1];
        k++;
    }
    a=0;    
}    

}
int main(){SeqList p;SeqList q;p.Insert(1,5);p.Insert(2,9);p.Insert(3,3);p.Insert(4,2);p.Insert(5,7);p.Sort();//p.Reverse();//p.Cancel(5);p.PrintList();return 0;}