回覆列表
  • 1 # 錢布斯

    #include <cstring>

    #include <cassert>

    #include <iostream>

    using namespace std;

    //定義的順序表類

    template <class T>

    class SqList

    {

    private:

    static const int MAX;

    int count;

    T data[MAX];

    public:

    SqList()

    :count(0)

    {

    }

    T& get(int idx)

    {

    assert(idx >= 0 && idx < count);

    return data[idx];

    }

    SqList<T>& insert(int idx, T elem)

    {

    assert(count < MAX);

    assert(idx >= 0 && idx <= count);

    int i;

    int j;

    for(i = count - 1; i >=idx; i--)

    data[i + 1] = data[i];

    data[idx] = elem;

    ++count;

    return *this;

    }

    {

    assert(idx >=0 && idx < count);

    int i;

    int j;

    T elem = data[idx];

    for(i = idx; i < count - 1; i++)

    data[i] = data[i + 1];

    --count;

    return elem;

    }

    {

    return remove(i + k - 1);

    }

    SqList<T>& push(T elem)

    {

    return insert(count, elem);

    }

    SqList<T>& operator <<(T elem)

    {

    return push(elem);

    }

    T& operator[] (int idx)

    {

    return get(idx);

    }

    int capacity() const

    {

    return count;

    }

    void output() const

    {

    int i;

    for(i = 0; i < count; i++)

    cout << data[i] << " " << endl;

    cout << endl;

    }

    };

    template <class T>

    const int SqList<T>::MAX = 20;

    int main()

    {

    SqList<int> sq;

    sq << 2 << 3 << 4 << 5;

    sq.output();

    sq.insert(1, 11);

    sq.output();

    sq.output();

    return 0;

    }

  • 中秋節和大豐收的關聯?
  • 《霹靂MIT》的演員表?