順序表的類模板定義

 //---------------------------------------ClassArrList.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;
}



//------------------------------------main fun.cpp-------------------------------------
#include "iostream"
#include "ClassArrList.h"
using namespace std;
int main()
{
    cout<<"----------------初始化順序表-----------------"<<endl;
    arrList <int> list(13);
    int input;
    int i;
    cout<<"請輸入6個數字:";
    for(i=0;i<6;i++)
    {
        cin>>input;
        list.append(input);
    }
    list.output();
    cout<<"--------------初始的順序表長度---------------"<<endl;
    cout<<"length of arrList:"<<list.length()<<endl;
    cout<<"-----------在第三位上插入一個數據------------"<<endl;
    list.insert(3,7);
    list.output();
    cout<<"-------------刪除第三位上的數據--------------"<<endl;
    list.delete1(3);
    list.output();
    cout<<"-------------修改第三爲上的數據--------------"<<endl;
    list.setValue(3,7);
    list.output();
    cout<<"------------返回第三爲上的數據值-------------"<<endl;
    int value;
    list.getValue(3,value);
    cout<<"The third value of the list is "<<value<<endl;
    cout<<"-----輸出執行了查找第一次位置操作的結果------"<<endl;
    int p;
    list.getPos(p,7);
    cout<<"7 is first appeared in list is the position "<<p<<endl;
    cout<<"----------------調用清空函數-----------------"<<endl;
    list.clear();
    cout<<"the longth of list is "<<list.length()<<endl;
    cout<<"----------------調用析構函數-----------------"<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章