顺序表

#include<bits/stdc++.h>
using namespace std;
int n;
template < class T>

class arrList
{
    private:
        T *aList;
        int maxSize;
        int curLen;
        int position;

    public:
        arrList(const  int s)
        {
            maxSize=s;
            aList=new T[maxSize];
            curLen=position=0;
        }
        ~arrList()
        {
            delete[] aList;
        }
        void Clear()
        {
            delete []aList;
            curLen=position=0;
            aList=new T[maxSize];
        }
        int length();
        bool Append(const T value); //表尾添加元素
        bool Insert(const int p,const T value);
        bool Delete(const int p);
        bool getValue(const int p, T &value);
        bool setValue(const int p,const T value);
        bool getPos(int &p,const T value);

};

template <class T>
bool arrList<T>::getPos(int &p,const T value)
{
    int i;
    for(i=0;i<n;i++)
        if(value == aList[i])
    {
        p=i;
        return 1;
    }
    return 0;
}

template <class T>
bool arrList<T>::Insert(const int p,const T value)
{
    int i;
    if(curLen>=maxSize)
    {
        cout<<"The list is overflow!"<<endl;
        return 0;
    }
    if(p<0||p>curLen)
    {
        cout<<"Insertion point is illegal"<<endl;
        return 0;
    }
    for(i=curLen;i>p;i--)
        aList[i]=aList[i-1];
    aList[p]=value;
    curLen++;
    return 1;
}

template <class T>
bool arrList<T>::Delete(const int p)
{
    int i;
    if(curLen<=0)
    {
        cout<<"No element to delete\n"<<endl;
        return 0;
    }
     if(p<0||p>curLen-1)
    {
        cout<<"deletion is illegal\n"<<endl;
        return 0;
    }
    for(i=p;i<curLen-1;i++)
        aList[i]=aList[i+1];
    curLen--;
    return 1;
}


int main()
{
    return 0;
}
















你可能感兴趣的:(顺序表)