您的位置:首页 > 其它

顺序表应用实例

2012-12-10 21:39 120 查看
#include "stdafx.h" 

template <class T>

class arrList

{                                       // 顺序表,向量

private:                               // 线性表的取值类型和取值空间

    T *aList ;                        // 私有变量,存储顺序表的实例

    int maxSize;                      // 私有变量,顺序表实例的最大长度

    int curLen;                        // 私有变量,顺序表实例的当前长度

    int position;                       // 私有变量,当前处理位置

public:                                // 顺序表的运算集

    arrList(const int size) 

    {                                  // 创建一个新的顺序表,参数为表实例的最大长度

        maxSize = size; aList = new T[maxSize]; curLen = position = 0;

    }

    ~arrList() 

    {                                   // 析构函数,用于消除该表实例

        delete [] aList;

        cout<<"析构函数"<<endl;

    }

    void clear() 

    {                                        // 将顺序表存储的内容清除,成为空表

        delete [] aList; curLen = position = 0;

        aList = new T[maxSize];

    }

    int length();                                      // 返回此顺序表的当前实际长度

    bool append(const T value);                        // 在表尾添加一个元素value,表的长度增1

    bool insert(const int p, const T value);           // 在位置p上插入一个元素value,表的长度增1

    bool delete1(const int p);                          // 删除位置p上的元素,表的长度减 1

    bool setValue(const int p, const T value);         // 用value修改位置p的元素值

    bool getValue(const int p, T& value);               // 把位置p的元素值返回到变量value中 

    bool getPos(int & p, const T value);               // 查找值为value的元素,并返回第1次出现的位置

    void output();

};

template <class T>

int arrList<T>::length()

{

    return curLen;

}

template <class T>

bool arrList<T>::append(const T value)

{

    aList[curLen++]=value;

    return true;

}

template <class T>

bool arrList<T>::insert(const int p, const T value)       // 在位置p上插入一个元素value,表的长度增1

{

    if(curLen>=maxSize)

        cout<<"The list is overflow!"<<endl;

    if(p<0||p>=maxSize)

        cout<<"The position is illegal!"<<endl;

    for(int i=curLen-1;i>=p;i--)

        aList[i+1]=aList[i];

    curLen++;

    aList[p]=value;

    return true;

}

template <class T>

bool arrList<T>::delete1(const int p)                        // 删除位置p上的元素,表的长度减 1

{

    if(curLen>=maxSize)

        cout<<"The list is overflow!"<<endl;

    if(p<0||p>=maxSize)

        cout<<"The position is illegal!"<<endl;

    for(int i=p;i<curLen-1;i++)

        aList[i]=aList[i+1];

    curLen--;

    return true;

}

template <class T> bool arrList<T>::setValue(const int p, const T value)         // 用value修改位置p的元素值

{

    if(curLen>=maxSize)

        cout<<"The list is overflow!"<<endl;

    if(p<0||p>maxSize)

        cout<<"The position is illegal!"<<endl;

    aList[p]=value;

    return true;

}

template <class T>

bool arrList<T>::getValue(const int p, T& value)               // 把位置p的元素值返回到变量value中 

{

    if(curLen>=maxSize)

        cout<<"The list is overflow!"<<endl;

    if(p<0||p>maxSize)

        cout<<"The position is illegal!"<<endl;

    value=aList[p];

    return true;

}

template <class T>

bool arrList<T>::getPos(int & p,const T value)                 // 查找值为value的元素,并返回第1次出现的位置

{

    if(curLen>=maxSize)

        cout<<"The list is overflow!"<<endl;

    for(int i=0;i<curLen;i++)

    {

        if(aList[i]==value)

        {

            p=i;

            break;

        }

    }

    return true;

}

template <class T>

void arrList<T>::output()                                 //输出顺序表函数

{

    for(int i=0;i<length();i++)

    {

        T value;

        getValue(i,value);

        cout<<value<<" ";

    }

    cout<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: