数据结构(六)

您所在的位置:网站首页 间接寻址是指指令中间 数据结构(六)

数据结构(六)

2024-07-17 01:50| 来源: 网络整理| 查看: 265

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

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

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

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

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

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

template class indirectList : public linearList { public: // 定义构造函数,复制构造函数和析构函数 indirectList(int maxSize = 10); indirectList(const indirectList&); ~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// 构造函数 indirectList :: indirectList(int maxSize) { this.maxSize = maxSize; listSize = 0; elements = new T*[maxSize]; } template indirectList :: indirectList(const indirectList& theList) { listSize = theList.listSize; maxSize = theList.maxSize; elements = new T*[maxSize]; T** temp = theList.elements; for(int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3