经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
数据结构(线性表)
来源:cnblogs  作者:Vaxy  时间:2018/9/25 20:39:12  对本文有异议

大二学C++都快忘没了,写点数据结构来复习一下,写的不好,不喜勿喷。

直接上代码,这是模板类的写法,必须全部写在头文件里。因为编译器不知道你会使用什么类型的数据,所以无法确定要分配的存储空间大小。


开发环境:CodeBlocks

  1. #ifndef LINEAR_LIST_H_INCLUDED
  2. #define LINEAR_LIST_H_INCLUDED
  3. #include <cstdio>
  4. template <class T>
  5. class List
  6. {
  7. private:
  8. int s; //线性表空间大小
  9. int length; //线性表元素个数
  10. T* head; //空间首地址
  11. public:
  12. List<T>() { }
  13. List<T>(int size) : s(size), head(NULL) { }
  14. ~List<T>() { DestroyList(); }
  15. void InitList(); //初始化线性表
  16. void DestroyList(); //销毁线性表
  17. void ClearList(); //线性表置空
  18. bool ListEmpty(); //判断线性表是否存在
  19. int ListLength(); //返回线性表长度
  20. bool GetElem(int i, T &e); //通过e得到第i个元素
  21. int LocateElem(T e, bool (*compare)(T list_e, T e)); //通过compare函数进行比较,得到第一个符合的元素
  22. bool PriorElem(T cur_e, T &pre_e); //通过pre_e得到cur_e的前驱
  23. bool NextElem(T cur_e, T &next_e); //通过next_e得到cur_e的后继
  24. bool ListInsert(int i, T e); //在第i个位置前插入新元素e
  25. bool ListDelete(int i, T &e); //删除第i个元素,用e返回它的值
  26. bool Insert(T); //向尾部插入一个元素
  27. bool ListTraverse(bool (*visit)(T e)); //依次对每个元素执行visit()操作
  28. };
  29. template <class T>
  30. void List<T>::InitList()
  31. {
  32. if(head == NULL)
  33. {
  34. head = new T[s];
  35. length = 0;
  36. }
  37. }
  38. template <class T>
  39. void List<T>::DestroyList()
  40. {
  41. if(head != NULL)
  42. {
  43. delete head;
  44. head = NULL;
  45. }
  46. }
  47. template <class T>
  48. void List<T>::ClearList()
  49. {
  50. length = 0;
  51. }
  52. template <class T>
  53. bool List<T>::ListEmpty()
  54. {
  55. if(head == NULL) return true;
  56. else return false;
  57. }
  58. template <class T>
  59. int List<T>::ListLength()
  60. {
  61. if(head == NULL) return -1;
  62. return length;
  63. }
  64. template <class T>
  65. bool List<T>::GetElem(int i, T &e)
  66. {
  67. if(head == NULL || length == 0) return false;
  68. e = head[i];
  69. return true;
  70. }
  71. template <class T>
  72. int List<T>::LocateElem(T e, bool (*compare)(T list_e, T e))
  73. {
  74. if(head == NULL || length == 0) return -1;
  75. for(int i = 0; i < length; i++)
  76. if(compare(head[i], e))
  77. return i;
  78. return -1;
  79. }
  80. template <class T>
  81. bool List<T>::PriorElem(T cur_e, T &pre_e)
  82. {
  83. if(head == NULL || length == 0 || head[0] == cur_e)
  84. return false;
  85. for(int i = 0; i < length; i++)
  86. {
  87. if(head[i] == cur_e)
  88. {
  89. pre_e = head[i-1];
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. template <class T>
  96. bool List<T>::NextElem(T cur_e, T &next_e)
  97. {
  98. if(head == NULL || length == 0 || head[length] == cur_e)
  99. return false;
  100. for(int i = 0; i < length; i++)
  101. {
  102. if(head[i] == cur_e)
  103. {
  104. next_e = head[i+1];
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. template <class T>
  111. bool List<T>::ListInsert(int i, T e)
  112. {
  113. if(head == NULL || length == 0 || length-1 < i || length == s)
  114. return false;
  115. for(int j = length-1; j >= i; j--)
  116. head[j+1] = head[j];
  117. head[i] = e;
  118. length++;
  119. return true;
  120. }
  121. template <class T>
  122. bool List<T>::ListDelete(int i, T &e)
  123. {
  124. if(head == NULL || length == 0 || length-1 < i)
  125. return false;
  126. if(i == length-1)
  127. {
  128. length--;
  129. e = head[i];
  130. return true;
  131. }
  132. e = head[i];
  133. length--;
  134. for(int j = i; j < length; j++)
  135. head[j] = head[j+1];
  136. return true;
  137. }
  138. template <class T>
  139. bool List<T>::Insert(T e)
  140. {
  141. if(head == NULL || length == s) return false;
  142. head[length] = e;
  143. length++;
  144. return false;
  145. }
  146. template <class T>
  147. bool List<T>::ListTraverse(bool (*visit)(T e))
  148. {
  149. if(head == NULL || length == 0)
  150. return false;
  151. for(int i = 0; i < length; i++)
  152. if(!visit(head[i]))
  153. return false;
  154. return true;
  155. }
  156. #endif // LINEAR_LIST_H_INCLUDED
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号