数据结构与算法C++描述(3)---间接寻址
来源:互联网 发布:seo营销工具 编辑:程序博客网 时间:2024/05/15 21:41
间接寻址就是线性表和链表的组合,它利用了线性表中的公式化描述,同时也利用了链表中的链接指针。。采用这种描述方法,可以保留公式化描述方法的许多优点——可以根据索引在
1、 对间接寻址的操作有:
2、C++实现间接寻址
2.1 创建
通过实现IndirectList类,实现间接寻址的相关操作。创建一个间接寻址是通过IndirectList类的构造函数实现的。代码如下(其中,maxListSize是最大的容量,大于此值,便会抛出“溢出”异常):
//新建一个列表template <class T>IndirectList<T>::IndirectList(int maxListSize=10){ MaxSize = maxListSize; table = new T*[MaxSize]; length = 0;}
2.2 删除
通过析构函数实现,由于table是指针类型的数组,即数组中每个元素都是是个指向列表中下一个元素的指针,因此需要首先删除每个指针后,在释放数组。:
template <class T>IndirectList<T>::~IndirectList(){ //删除表 for (int i = 0; i < length; i++) delete table[i]; delete[] table;}
2.3 寻找第k个位置的元素
寻找第k个位置的元素,若存在,则将值赋给x,并返回true;否则,返回false。
template <class T> bool IndirectList<T>::Find(int k, T &x) const{ if (k > length || k<1) { throw OutOfRange(); return false; } else { x = *table[k-1]; return true; }}
2.4 寻找列表中值为x的元素
寻找列表中值为x的元素,若存在,则返回位置,否则,返回0
template <class T>int IndirectList<T>::Search(const T&x) const{ int i = 1; for (i = 1; i <= length; i++) { if (*table[i - 1] == x) return i; else continue; } return 0;}
2.5 删除表中第k个元素
删除表中第k个元素,并將值赋给x,并返回操作后的列表
template <class T>IndirectList<T>& IndirectList<T>::Delete(int k, T &x){ if (Find(k, x)) { //往前移 for (int i = k; i <= length; i++) table[i - 1] = table[i]; length--; return *this; } else throw OutOfRange();}
2.6 在第k个位置之后插入元素x
在第k个位置之后插入元素x,并并返回操作后的列表
template <class T>IndirectList<T>& IndirectList<T>::Insert(int k, const T &x){ if (k<0 || k>length) throw OutOfRange(); else if (k == MaxSize) throw NoMerm(); else { //往后移 for (int i = 0; i <= length - k; i++) table[length - i + 1] = table[length - i]; table[k] = new T; *table[k] = x; length++; return *this; }}
2.7 输出
通过重载“<<”运算符实现
//重载“<<”template <class T>ostream &operator<<(ostream &out, const IndirectList<T> &x){ x.Output(out); return out;}//输出列表函数template <class T>void IndirectList<T>::Output(ostream &out) const{ for (int i = 0; i < length; i++) out << *table[i] << " ";}
3、测试样例
IndirectList<int> inList; //测试构造函数 cout <<"建立初始间接寻址: " <<inList.Length() << endl; //测试Insert函数 cout << "Insert(1,1).Insert(2,2).Insert(3,3): " << inList.Insert(0, 1).Insert(1,2).Insert(2,3) << endl; cout << "Insert(2,5): " << inList.Insert(2, 5) << endl; //测试Delete函数 int x; inList.Delete(2, x); cout << "Delete(2,x): " << x <<" "<<inList<< endl; //测试Find函数 inList.Find(2, x); cout << "Find(2,x): " << x << " " << inList << endl; //cout << "Find(5,x): " << x << " " << inList.Find(5, x) << endl; //测试Search函数 cout << "Search(2): " << inList.Search(2) << " " << inList << endl; cout << "Search(1): " << inList.Search(1) << " " << inList << endl;
参考文献:
[1] 数据结构算法与应用:C++描述(Data Structures, Algorithms and Applications in C++ 的中文版)
阅读全文
0 0
- 数据结构与算法C++描述(3)---间接寻址
- 数据结构实验(二):间接寻址
- 数据结构之间接寻址
- C语言指针 直接寻址与间接寻址详解
- 经典数据结构之间接寻址
- 数据结构|创建学生成绩的间接寻址(实验2.5)
- 数据结构|创建学生成绩的间接寻址(实验2.5)
- 数据结构与算法分析(C语言描述)L1.1
- 数据结构与算法(C语言描述)——单链表
- 读《算法与数据结构---c语言描述》
- 算法与数据结构描述
- C++_009_数据结构_线性表_间接寻址方式储存
- 数据结构与算法描述(习题3答案)
- 操作数的寻址方式(寄存器间接,基址寻址,间址寻址,比例间接寻址)
- 间接寻址
- 间接寻址
- 间接寻址
- 排序 - C语言实现(摘自数据结构与算法分析C语言描述))
- Tomcat部署时war和war exploded区别
- 乱码问题
- [BZOJ]1819: [JSOI]Word Query电子字典 trie+链表
- Java Double类型如何不科学计数法显示
- 设计模式——代理模式
- 数据结构与算法C++描述(3)---间接寻址
- Mybaits架构设计及其原理剖析
- Unity物体绕圆运动-轨迹
- python-在Windows下使用virtualenv
- OSI参考模型中各层的作用
- Eclipse中的项目名后NO-HEAD、项目名后不显示maven.日期版本号的问题
- WM修改屏幕分辨率 —加快手机运行速度—一机也能做适配
- YTU.1317: 控制台表格
- IO其他流--处理流保留数据+引用数据类型