シーヶンステーブルクラス

1831 ワード

/*
 ( 、 )
*/
#include <iostream>
#include <cstdio>
using namespace std;

template <class T>
class Sqlist
{
    private:
        int curlen;
        int maxlen;
        T *elem;
    public:
        Sqlist( T *a,int n,int maxsize=100 ):maxlen(maxsize)
        {
            int i;
            curlen=n;
            elem=new T[curlen];
            for( i=0;i<n;i++ )
                elem[i]=a[i];
        }
        int loct( T a );
        void dele( int loc );
        void inst( int loc,int num );
        void print();
};

template <class T>
int Sqlist<T>::loct( T a )
{
    int i;
    for( i=0;i<curlen;i++ )
        if( elem[i]==a )
            return (i+1);
    return 0;
}

template <class T>
void Sqlist<T>::dele( int loc )
{
    int i;
    if( loc<1 || loc>curlen )
    {
        cout<<"Fail!"<<endl;
        return;
    }
    for( i=loc-1;i<curlen-1;i++ )
        elem[i]=elem[i+1];
    curlen--;
    cout<<"Succeed!"<<endl;
}

template <class T>
void Sqlist<T>::inst( int loc,int num )
{
    int i;
    if( loc<1 || loc>curlen+1 || curlen>=maxlen )
    {
        cout<<"Fail!"<<endl;
        return;
    }
    for( i=curlen;i>=loc;i-- )
        elem[i]=elem[i-1];
    elem[loc-1]=num;
    curlen++;
    cout<<"Succeed!"<<endl;
}

template <class T>
void Sqlist<T>::print()
{
    int i;
    for( i=0;i<curlen;i++ )
        cout<<elem[i]<<" ";
    cout<<endl;
}

int main()
{
    int i;
    int a[10]={ 1,2,304,43,32,36,40,66,91,82 };
    Sqlist<int> test( a,10 );
    cout<<test.loct(43)<<endl;
    test.dele(4);
    test.inst(4,34);
    test.print();
    return 0;
}