最近阅读《数据结构、算法及应用》这本书,书中的习题汇总,用自己的方法来实现这些问题。可能效率。等方面存在着非常多的问题,也可能是错误的实现。假设大家在看这本书的时候有更优更好的方法来实现,还请大家多多留言交流多多指正,谢谢

9. C++实现的线性表--顺序表

  1. //
  2. // LinearList.h
  3. // LinearList
  4. //
  5. // Created by cc on 14-8-21.
  6. // Copyright (c) 2014年 cc. All rights reserved.
  7. // 顺序表
  8.  
  9. #ifndef __LinearList__LinearList__
  10. #define __LinearList__LinearList__
  11.  
  12. #include <iostream>
  13. #include <stdlib.h>
  14.  
  15. using namespace std;
  16.  
  17. template <class T>
  18. class LinearList {
  19.  
  20. private:
  21.  
  22. //顺序表长度
  23. int m_length;
  24. //顺序表最大的长度
  25. int m_maxSize;
  26. //元素数组
  27. T* m_element;
  28.  
  29. public:
  30.  
  31. LinearList(int maxListSize);
  32. virtual ~LinearList();
  33.  
  34. /**
  35. * @brief 顺序表是否为空
  36. *
  37. * @return true: 空 false: 非空
  38. */
  39. bool isEmpty() const;
  40.  
  41. /**
  42. * @brief 获取顺序表的长度
  43. *
  44. * @return 顺序表的长度
  45. */
  46. int length() const;
  47.  
  48. /**
  49. * @brief 获取顺序表最大容量
  50. *
  51. * @return 顺序表的最大容量
  52. */
  53. int maxSize() const;
  54.  
  55. /**
  56. * @brief 在顺序表中查找第k个元素。并返回第k个元素至x中
  57. *
  58. * @param k 第k个元素
  59. * @param x 保存第k个元素
  60. *
  61. * @return true: 找到了第k个元素 false: 没找到第k个元素
  62. */
  63. bool find(int k, T& x) const;
  64.  
  65. /**
  66. * @brief 查找顺序表中的元素x, 而且返回x所在位置
  67. *
  68. * @param x 元素x
  69. *
  70. * @return 元素x的在顺序表中的位置
  71. */
  72. int search(const T& x) const;
  73.  
  74. /**
  75. * @brief 删除第k个元素并将它返回至x中
  76. *
  77. * @param k 第k个元素
  78. * @param x 保存第k个元素
  79. *
  80. * @return 改动后的顺序表
  81. */
  82. LinearList<T>& deleteElement(int k, T& x);
  83.  
  84. /**
  85. * @brief 在第k个元素之后插入x元素
  86. *
  87. * @param k 第k个元素
  88. * @param x 元素x
  89. *
  90. * @return 改动后的顺序表
  91. */
  92. LinearList<T>& insertElement(int k, const T& x);
  93.  
  94. /**
  95. * @brief 输出信息
  96. *
  97. * @param out 输出流
  98. */
  99. void output(ostream& out) const;
  100.  
  101. };
  102.  
  103. template <class T>
  104. LinearList<T>::LinearList(int maxListSize):m_length(0), m_element(NULL) {
  105. this->m_maxSize = maxListSize;
  106. this->m_element = new T[this->m_maxSize];
  107. }
  108.  
  109. template <class T>
  110. LinearList<T>::~LinearList() {
  111. delete [] this->m_element;
  112. this->m_element = NULL;
  113. }
  114.  
  115. /**
  116. * @brief 顺序表是否为空
  117. *
  118. * @return true: 空 false: 非空
  119. */
  120. template <class T>
  121. bool LinearList<T>::isEmpty() const {
  122. return this->m_length == 0;
  123. }
  124.  
  125. /**
  126. * @brief 获取顺序表的长度
  127. *
  128. * @return 顺序表的长度
  129. */
  130. template <class T>
  131. int LinearList<T>::length() const {
  132. return this->m_length;
  133. }
  134.  
  135. /**
  136. * @brief 获取顺序表最大容量
  137. *
  138. * @return 顺序表的最大容量
  139. */
  140. template <class T>
  141. int LinearList<T>::maxSize() const {
  142. return this->m_maxSize;
  143. }
  144.  
  145. /**
  146. * @brief 在顺序表中查找第k个元素,并返回第k个元素至x中
  147. *
  148. * @param k 第k个元素
  149. * @param x 保存第k个元素
  150. *
  151. * @return true: 找到了第k个元素 false: 没找到第k个元素
  152. */
  153. template <class T>
  154. bool LinearList<T>::find(int k, T& x) const {
  155. if (k > this->m_length - 1
  156. || k < 0) {
  157. cout << "在顺序表中为找到第" << k << "个位置上的元素";
  158. return false;
  159. }
  160.  
  161. x = m_element[k];
  162. return true;
  163. }
  164.  
  165. /**
  166. * @brief 查找顺序表中的元素x, 而且返回x所在位置(位置是下标+1)
  167. *
  168. * @param x 元素x
  169. *
  170. * @return 元素x的在顺序表中的位置
  171. */
  172. template <class T>
  173. int LinearList<T>::search(const T& x) const {
  174. for (int i = 0; i < this->m_length; i++) {
  175. if (this->m_element[i] == x) {
  176. return i;
  177. }
  178. }
  179. return 0;
  180. }
  181.  
  182. /**
  183. * @brief 删除第k个元素并将它返回至x中
  184. *
  185. * @param k 第k个元素
  186. * @param x 保存第k个元素
  187. *
  188. * @return 改动后的顺序表
  189. */
  190. template <class T>
  191. LinearList<T>& LinearList<T>::deleteElement(int k, T& x) {
  192. if (find(k, x)) {
  193. //找到了第k个元素
  194. for (int i = k; i < this->m_length - 1; i++) {
  195. this->m_element[i] = this->m_element[i + 1];
  196. }
  197. this->m_length--;
  198. } else {
  199. // throws exception
  200. cout << "未找到第" << k << "个元素";
  201. }
  202. return *this;
  203. }
  204.  
  205. /**
  206. * @brief 在第k个元素之后插入x元素
  207. *
  208. * @param k 第k个元素
  209. * @param x 元素x
  210. *
  211. * @return 改动后的顺序表
  212. */
  213. template <class T>
  214. LinearList<T>& LinearList<T>::insertElement(int k, const T& x) {
  215.  
  216. if (k > m_maxSize - 1
  217. || k < 0) {
  218. cout << "数组下标越界!" << endl;
  219. // throws OutOfBoundsException
  220. } else if (this->m_length == this->m_maxSize) {
  221. cout << "已达到数组最大容量。申请内存后再加入!" << endl;
  222. // throws NoMemException
  223. } else {
  224. //找到了第k个元素
  225. for (int i = this->m_length - 1; i > k; i--) {
  226. this->m_element[i] = this->m_element[i - 1];
  227. }
  228. this->m_length++;
  229. this->m_element[k] = x;
  230. }
  231.  
  232. return *this;
  233. }
  234.  
  235. template<class T>
  236. void LinearList<T>::output(ostream& out) const {
  237. for (int i = 0; i < this->m_length; i++)
  238. out << this->m_element[i] << " ";
  239. }
  240.  
  241. // overload
  242. template <class T>
  243. ostream& operator<<(ostream& out, const LinearList<T>& x){
  244. x.output(out); return out;
  245. }
  246.  
  247. #endif /* defined(__LinearList__LinearList__) */
  1. //
  2. // main.cpp
  3. // LinnerList
  4. //
  5. // Created by ChengChao on 14-8-29.
  6. // Copyright (c) 2014年 cc. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include "LinearList.h"
  12.  
  13. int main(int argc, const char * argv[]) {
  14.  
  15. LinearList<int> list(10);
  16.  
  17. list.insertElement(0, 1000).insertElement(1, 312).insertElement(2, 134);
  18. cout << "The list is" << endl;
  19. cout << list << endl;
  20.  
  21. //顺序表是否为空
  22. cout << "顺序表是否为空:" << boolalpha << list.isEmpty() << endl;
  23. cout << "顺序表的最大容量:" << list.maxSize() << endl;
  24.  
  25. int res = 0;
  26. int res2 = 0;
  27. //查找99
  28. cout << "查找第0个元素输入到res变量中。是否找到: " << boolalpha << list.find(0, res) << "。res=" << res << endl;
  29. //查找312
  30. cout << "查找第5个元素输入到res变量中。是否找到: " << boolalpha << list.find(5, res2) << ",res=" << res2 << endl;
  31.  
  32. //删除第1个位置上的元素元素
  33. list.deleteElement(1, res);
  34. cout << list << endl;
  35.  
  36. //查找元素134
  37. cout << "搜索元素134,位置为:" << list.search(134) << endl;
  38.  
  39. return 0;
  40. }
&#8;输出结果例如以下图:


注意C++中的模板不支持编译分离,须要把模板类的声明和实现放到.h文件中面 

本文由CC原创总结。如需转载请注明出处:http://blog.csdn.net/oktears/article/details/27966399

版权声明:本文博主原创文章,博客,未经同意不得转载。

《数据结构、算法及应用》9.(C++实施订单)的更多相关文章

  1. 初转java随感(一)程序=数据结构+算法

    大学刚学编程的时候,有一句很经典的话程序=数据结构+算法 今天有了进一步认识. 场景: 1.当前局面 (1)有现成的封装好的分页组件 返回结果是page.类型为:Page.包括 page 分页信息,d ...

  2. 数据结构算法集---C++语言实现

    //数据结构算法集---C++语言实现 //各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点) /////////////////////////// // // // 堆栈数据结构 s ...

  3. 数据结构+算法面试100题~~~摘自CSDN

    数据结构+算法面试100题~~~摘自CSDN,作者July 1.把二元查找树转变成排序的双向链表(树) 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调 ...

  4. day40 数据结构-算法(二)

    什么是数据结构? 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表.集合与字典等都是一种数据结构. N.Wirth: “程序=数据结构+算法” 列表 列表:在其他编程语言中称 ...

  5. 前端要不要学数据结构&算法

    我们都知道前端开发工程师更多偏向 DOM 渲染和 DOM 交互操作,随之 Node 的推广前端工程师也可以完成服务端开发.对于服务端开发而言大家都觉得数据结构和算法是基础,非学不可.所以正在进行 No ...

  6. C语言版数据结构算法

    C语言版数据结构算法 C语言数据结构具体算法 https://pan.baidu.com/s/19oLoEVqV1I4UxW7D7SlwnQ C语言数据结构演示软件 https://pan.baidu ...

  7. 【数据结构与算法】多种语言(VB、C、C#、JavaScript)系列数据结构算法经典案例教程合集目录

    目录 1. 专栏简介 2. 专栏地址 3. 专栏目录 1. 专栏简介 2. 专栏地址 「 刘一哥与GIS的故事 」之<数据结构与算法> 3. 专栏目录 [经典回放]多种语言系列数据结构算法 ...

  8. 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值

    一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...

  9. 数据结构算法C语言实现(六)---2.4一元多项式的表示及相加

    一.简述 利用链表表示稀疏多项式,并基于之前的一些操作(编程实现上还是有所不同的)组合新的操作实现一元多项式的表示及相加. 二.ADT 抽象数据类型一元多项式的定义 ADT Polyomail{ 数据 ...

随机推荐

  1. 使用HtmlAgilityPack批量抓取网页数据

    原文:使用HtmlAgilityPack批量抓取网页数据 相关软件点击下载登录的处理.因为有些网页数据需要登陆后才能提取.这里要使用ieHTTPHeaders来提取登录时的提交信息.抓取网页  Htm ...

  2. Android中&lt;meta-data&gt;的使用

    在AndroidManifest.xml中.<meta-data>元素能够作为子元素,被包括在<activity>.<application> .<servi ...

  3. hdu5179(数位dp)

    传送门:beautiful number 题意:令 A=∑ni=1ai?10n?i(1≤ai≤9)(n为A的位数).若A为“漂亮的数”当且仅当对于任意1≤i<n满足a[i]≥a[i+1]且对于任 ...

  4. hdu2063+hdu1083(最大匹配数)

    传送门:hdu2063过山车 #include <cstdio> #include <cstring> #include <string> #include < ...

  5. HDU 1016 Prime Ring Problem 题解

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  6. 《WordPress插件开发手冊》文件夹

    翻译前言:国内没有关于WordPress插件开发比較具体而且系统的资料 前言 第一章:准备一个本地开发环境 介绍 在你的电脑上安装一个站点server 下载并配置一个本地的WordPress 创建一个 ...

  7. A Game of Thrones(3) - Daenerys

    Her brother held the gown up for her inspection. “This is beauty. Touch it. Go on. Caress(爱抚,抚抱) the ...

  8. cocos2d-x2.2.3和android平台环境的搭建

    准备工作:1.我只是将cocos2d-x移植到android平台,所以默认为大家已经将android平台搭建完成了(eclipse和android SDK已经配置好,java环境搭建好) 2.下载an ...

  9. WIFI实时监控追踪小车演示视频——安卓端、小车

    文章由@超人爱因斯坦出品,转载请注明出处.         文章链接:          http://hpw123.net/a/qingsongyike/yingyinqu/2014/1019/59 ...

  10. 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...