数据结构(六)——间接寻址

其实在作为链表成员函数的箱子排序中已经用到间接寻址,即指向指针的指针。

chianNode<T> **bottom, **top
bottom = new chainNode<T>* [range+1];
top = new chainNode<T>* [range+1];

这里的bottom保存的数据类型为chainNode* 的指针类型,指向指针的bottom便实现了间接寻址。

因为书上没有用间接寻址实现线性表的代码,但是考试大纲上有提到。博主便参考多个案例加上自己的理解实现利用间接寻址实现线性表的代码,如有错误,欢迎指正。

间接寻址实现线性表的优势:间接寻址中,数组不在保存节点,而是保存节点的地址,可以看做是公式化描述和链式描述的结合体。存取时间复杂度为O(1),插入删除操作仅仅修改节点的地址在数组中的位置,优于使用链表描述的插入删除操作。
缺陷:保存节点的地址增加了额外的开销

实现代码:
代码中默认已经做好了*、=和必要操作符的重载

template<class T>

class indirectList : public linearList<T>
{
public:

    // 定义构造函数,复制构造函数和析构函数
    indirectList(int maxSize = 10);
    indirectList(const indirectList<T>&);
    ~indirectList();

    //抽象类linearList定义的方法
    bool empty() const { return listSize == 0; }
    int size() const { return listSize; }
    T& get(int theIndex) const;
    int indexof(const T& theElement) const;
    void erase(int theIndex);
    void insert(int theIndex, const T& theElement);
    void output(ostream& out) const;

private:

    int listSize;
    int maxSize;
    T** elements;

};

template<class T>// 构造函数
indirectList<T> :: indirectList(int maxSize)
{
    this.maxSize = maxSize;
    listSize = 0;
    elements = new T*[maxSize];
}

template<class T>
indirectList<T> :: indirectList(const indirectList& theList)
{
    listSize = theList.listSize;
    maxSize = theList.maxSize;
    elements = new T*[maxSize];
    T** temp = theList.elements;

    for(int i = 0; i<listSize; i++)
    {
        *elements[i] = *temp[i];
    }
}

template<class T>// 析构函数
indirectList<T> :: ~indirectList()
{
    for(int i = 0; i < listSize; i++)
    {
        delete elements[i];
    }
    delete [] elements;
}

template<class T>
T& indirectList<T> :: get(int theindex) const
{
    if(theindex > listSize || theindex < 1)
        throw "the index is out of range.";

    return *elements[theindex-1];
}

template<class T>
int indirectList<T> :: indexof(const T& theElement) const
{
    for(int i = 0; i < listSize; i++)
    {
        if(*elements[i] == theElement)
            return i+1;
    }
    return -1;
}

template<class T>
void indirectList<T> :: erase(int theindex)
{
    if(theindex > listSize || theindex < 1)
        throw "the index is out of range.";

    for(int i = theindex - 1; i< listSize - 1; i++)
    {
        elements[i] = elements[i+1];
    }
    listSize--;
}
template<class T>
void indirectList<T> :: insert(int theindex, const T& theElement)
{
    if(theindex > listSize || theindex < 1)
        throw "the index is out of range.";

    if(listSize == maxSize)
    {
        T** temp = new T*[maxSize*2];
        copy(elements, elements+maxSize, temp);// STL copy函数
        for(int i = 0; i<maxSize; i++)
            delete elements[i];
        delete [] elements;

        elements = temp;
        maxSize = maxSize*2;
    }
    for(int i = theindex - 1; i > theindex - 1; i--)
    {
        elements[i+1] = elements[i];
    }

    elements[theindex-1] = new T;
    *elements[theindex-1] = theElement;
}

template<class T>
void indirectList<T> :: output(ostream& out) const
{
    for(int i = 0; i < listSize; i++)
    {
        out << *elements[i] << " ";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41882686/article/details/106743872