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

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] << " ";}
}

数据结构(六)——间接寻址相关推荐

  1. 数据结构六——堆的应用

    文章出处:极客时间<数据结构和算法之美>-作者:王争.该系列文章是本人的学习笔记. 堆比较适合动态数据的场景. 1 应用一:优先级队列 一个优先级队列就是一个堆. 1.1 合并小文件 假设 ...

  2. 【技术点】数据结构(六) -- 哈希表

    文章目录 简介 哈希函数 直接寻址法 取模法 其他 冲突的处理 链地址法 开放地址法 其他 实例 JAVA中的哈希表 Entry对象 & 哈希映射方法 映射计算方法 冲突解决办法 - Hash ...

  3. 算法与数据结构(六) 迪杰斯特拉算法的最短路径(Swift版)

    上篇博客我们详细的介绍了两种经典的最小生成树的算法,本篇博客我们就来详细的讲一下最短路径的经典算法----迪杰斯特拉算法.首先我们先聊一下什么是最短路径,这个还是比较好理解的.比如我要从北京到济南,而 ...

  4. 数据结构(六)霍夫曼树与编码

    1.算法流程 (1)构建霍夫曼树:自底向上 根据统计频率构建霍夫曼树: A.把所有的节点扔进排序队列queue中: B.从queue选择选择前面两个最小的元素a.b,把最小的树a作为左节点,把最小的b ...

  5. 算法与数据结构(六):堆排序

    上一次说到了3种基本的排序算法,三种基本的排序算法时间复杂度都是O(n^2),虽然比较简单,但是效率相对较差,因此后续有许多相应的改进算法,这次主要说说堆排序算法. 堆排序算法是对选择排序的一种优化. ...

  6. python数据结构视频百度云盘_数据结构与算法Python视频领课

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 课程简介: 本课程包含Python编程基础的基本语法及变量,基本数据结构,Code Structure,Function.让学生在学会Python基础的同 ...

  7. Redis数据结构Hash应用场景-存储商品、购物车、淘宝短链接、分布式Session、用户注册、发微博功能

    Hash应用场景 Hash Hash应用场景 redis存储java对象常用String,那为什么还要用hash来存储? SpringBoot+redis+hash存储商品数据 短链接 场景1:淘宝短 ...

  8. [数据结构与算法综合实验]欢乐练练看

    开源项目:wut-matching-game 开发时间:2021.05 - 2021.06 文章目录 一.快速开始 二.游戏介绍 三.游戏规则 四.功能简介 五.数据结构 六.功能演示 1. 欢迎界面 ...

  9. ApacheCN C/C++ 译文集(二) 20211204 更新

    编写高效程序的艺术 零.序言 第一部分:性能基础 一.性能和并发性介绍 二.性能测量 三.CPU 架构.资源和性能 四.内存架构和性能 五.线程.内存和并发 第二部分:高级并发 六.并发和性能 七.并 ...

  10. 详解Linux-I2C驱动

    目录 一.LinuxI2C驱动--概述 1.1 写在前面 1.2 I2C 1.3 硬件 1.4 软件 1.5 参考 二.LinuxI2C驱动--I2C总线 2.1 I2C总线物理结构 2.2 I2C总 ...

最新文章

  1. Python之Python 安装环境搭建
  2. Qt之excel 操作使用说明
  3. Binder实用指南(二) - 实战篇
  4. Android之android8.1打开热点提示UID 10140 does not have Location permission和Location mode is enabled.
  5. Exynos4412 Uboot 编译工具 —— 交叉工具链 arm-linux-gcc 的安装
  6. 爬虫技术实战 | WooYun知识库
  7. js与c语言效率_JavaScript比c语言的性能差了多少?
  8. mac上python无法import redis
  9. Redis学习笔记001---Windows下安装Redis
  10. Ajax异步获取html数据中包含js方法无效的解决方法
  11. ajax上传.mp4文件不出错,ajax视频如何上传?
  12. 深度优先遍历(DFS)- Letter CasePermutation - Combinations
  13. ubuntu16.04 运行dso问题梳理
  14. 推荐微软Windows 8 Metro应用开发虚拟实验室
  15. VMProtect修复导入表的插件
  16. 中专计算机应用专业简历模板,2016计算机应用专业个人简历模板
  17. XlsxWriter的使用
  18. 债券价格和到期收益率的关系_债券价格与到期收益率之间的关系.PPT
  19. hadoop学习之路(2)
  20. 手把手教你:CSS+JS 打造一个有个性的滚动条

热门文章

  1. 程序员 你努力的方向对吗?
  2. Jenkins Pipeline 手记(1)—— 什么是CPS编程
  3. find_calib_object算子说明
  4. 档案管理学川大972 | 档案信息资源开发与利用
  5. 电子科技大学软件工程860考研专业课真题考频总结
  6. python爬虫之淘宝秒抢软件
  7. 人工神经网络评价法案例_人工神经网络评价法.
  8. 站群服务器找11火星软件
  9. 使用微信开发工具开发微信小程序(二)——协同工作、发布与事件绑定
  10. 双绞线的制作,T568A线序,T568B线序