SparseVector.hh

  1. class SparseVector
  2. {
  3. private:
  4. //结构体不一定会用到,不用初始化
  5. struct node
  6. {
  7. int index;
  8. int value;
  9. node *next;
  10.  
  11. node(int index, int value, node *next = ) : index(index), value(value), next(next) {}
  12. };
  13. //这些才是真正的数据成员,要初始化的
  14. int size;
  15. node *start;
  16.  
  17. void clear();
  18. void copyList(const SparseVector &sv);
  19. void setNonzeroElem(int index, int value);
  20. void removeElem(int index);
  21. void checkListOrder();
  22. public:
  23. SparseVector(int size);
  24. const int getSize()const;
  25.  
  26. ~SparseVector();
  27. SparseVector(const SparseVector &sv);
  28. SparseVector & operator= (const SparseVector &sv);
  29. int getElem(int idx);
  30. void setElem(int index, int value);
  31.  
  32. };

SparseVector.cc

  1. #include "SparseVector.hh"
  2. #include <cassert>
  3. #include <iostream>
  4. using namespace std;
  5. //单参数构造函数
  6. SparseVector::SparseVector(int size):size(size)
  7. {
  8. start = ;
  9. }
  10.  
  11. const int SparseVector::getSize()const
  12. {
  13. return size;
  14. }
  15. //成员函数都默认带有this指针,所以默认对调用这个函数的对象进行操作,所以不用再传本对象的地址了。
  16. void SparseVector::clear()
  17. {
  18. node *next;
  19. node *current;
  20. current = start;
  21. while(current != )
  22. {
  23. next = current->next;
  24. delete current;
  25. current = next;
  26. }
  27. start = ;
  28. }
  29. //对本对象进行操作,调用成员行数也是默认对本对象进行操作,不用传本对象地址。
  30. SparseVector::~SparseVector()
  31. {
  32. clear();
  33. }
  34.  
  35. void SparseVector::copyList(const SparseVector &sv)
  36. {
  37. size = sv.getSize();
  38. node *current;
  39. node *otherCurrent =sv.start;
  40. node *prev = ;
  41.  
  42. while(otherCurrent != )
  43. {
  44. current = new node(otherCurrent->index, otherCurrent->value);
  45. if(prev == )
  46. {
  47. start = current;
  48. prev = current;
  49. }
  50. prev->next = current;
  51. prev = current;
  52. otherCurrent = otherCurrent->next;
  53. }
  54. }
  55.  
  56. SparseVector::SparseVector(const SparseVector &sv)
  57. {
  58. copyList(sv);
  59. }
  60. //注意自赋值,并且直接调用私有帮助函数。
  61. SparseVector & SparseVector:: operator= (const SparseVector &sv)
  62. {
  63. if (this == &sv)
  64. {
  65. return *this;
  66. }
  67. clear();
  68. copyList(sv);
  69. return *this;
  70. }
  71. //难点
  72. int SparseVector::getElem(int idx)
  73. {
  74. node *current = start;
  75. while(current != && current->index < idx)//过滤,两个条件
  76. {
  77. current = current->next;
  78. }
  79. if(current == )//注意判断条件先后次序,先排除current为0情况
  80. {
  81. return ;
  82. }
  83. else if(current->index == idx)//如果先执行这个,则current为0时,会直接产生段错误
  84. {
  85. return current->value;
  86. }
  87. else
  88. {
  89. return ;
  90. }
  91. }
  92. //难点,分种情况讨论:1,初始为空。2,插到最后面。3,插到最前面。4,插到中间。
  93. void SparseVector::setNonzeroElem(int index, int value)
  94. {
  95. assert(value != );
  96. node *current = start;
  97. node *prev = ;
  98.  
  99. if(start == )//容易遗漏,链表初始为空的情况。(1)
  100. {
  101. start = new node(index, value);
  102. }
  103. else//除此情况外(2,3,4)
  104. {
  105. while(current != && current->index < index)//过滤,两个条件,保证current指向应该指的结点,或其之后的结点。prev指向值小于应该的结点。
  106. {
  107. prev = current;
  108. current = current->next;//别忘了自增
  109. }
  110. /*2选1
  111. * if(current == start)//插到最前面,current所指结点大于等于它
  112. {
  113. if(current->index == index)//等于
  114. {
  115. current->value = value;
  116. }
  117. else//大于
  118. {
  119. node *other = new node(index, value, start);
  120. start = other;
  121. }
  122. }
  123. else if(current == 0)//插到最后面,current所指结点小于它
  124. {
  125. node *other = new node(index, value, 0);
  126. prev->next = other;
  127. }
  128. else//插到中间,current所指结点大于等于它
  129. {
  130. if(current->index == index)//current所指结点等于它
  131. {
  132. current->value = value;
  133. }
  134. else//current所指结点结点大于它
  135. {
  136. node *other = new node(index, value, current);
  137. prev->next = other;
  138. }
  139. }
  140. */
  141. if(current == )//插到最后边
  142. {
  143. node *other = new node(index, value);
  144. prev->next = other;
  145. }
  146. else if(current -> index == index)//current所指结点等于它的值
  147. {
  148. current->value =value;
  149. }
  150. else if(current == start)//在最开始的地方
  151. {
  152. node *other = new node(index, value, start);
  153. start = other;
  154. }
  155. else //在中间
  156. {
  157. node *other = new node(index, value, current);
  158. prev->next = other;
  159. }
  160. }
  161. }
  162.  
  163. void SparseVector::removeElem(int index)
  164. {
  165. node *current = start;
  166. node *prev = ;
  167. while(current != && current->index < index)//过滤
  168. {
  169. prev = current;
  170. current = current->next;
  171. }
  172. if(current->index == index)//如果是这个结点
  173. {
  174. if(current == start)//是开始结点
  175. {
  176. prev = current;
  177. current = current->next;
  178. delete prev;
  179. start = current;
  180. return;
  181. }
  182. else//是中间结点或者是后边的节点(相同的)
  183. {
  184. prev->next = current->next;
  185. delete current;
  186. return;
  187. }
  188. }
  189. else
  190. {
  191. return;
  192. }
  193. }
  194.  
  195. void SparseVector::setElem(int index, int value)
  196. {
  197. if(value != )
  198. {
  199. setNonzeroElem(index, value);
  200. }
  201. else
  202. {
  203. removeElem(index);
  204. }
  205. }
  206.  
  207. void SparseVector::checkListOrder()
  208. {
  209. node *current = start;
  210. while(current != )
  211. {
  212. cout<<"("<<current->index<<" | "<<current->value<<")"<<endl;
  213. current = current->next;
  214. }
  215. return;
  216. }

cs11_c++_lab4a的更多相关文章

  1. cs11_c++_lab7

    wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...

  2. cs11_c++_lab6

    expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...

  3. cs11_c++_lab5待修改

    heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...

  4. cs11_c++_lab4b

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  5. cs11_c++_lab3

    Matrix.hh class Matrix { int row; int col; int *p; void copy(const Matrix &m); void clearup(); p ...

  6. cs11_c++_lab2

    Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...

  7. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

随机推荐

  1. 2016HUAS_ACM暑假集训4B - 递推

    这种数学推理题目题意极其明显,在做的时候,可以多写几组,这样找起规律来会容易些.概括起来就是:题意简单暴力,案例毫无价值. 一个三角形最多可以把一个平面分成两部分,两个三角形最多是8(2+6)部分,而 ...

  2. kuangbin_SegTree D (POJ 2528)

    讲道理我之前暂时跳过染色的题是因为总觉得有什么很高端的算法来query 直到我做了F题(ZOJ 1610)才发现就是个暴力统计.....也对 也就几万个长度单位而已.... F就不po上来了 选了有点 ...

  3. Ret2Libc 练习(2) -- VirtualProtect

    这几天做了NSCTF和GCTF,耽误了几天,今天继续. 这次绕过DEP的方法是利用VirtualProtect函数将shellcode所在的内存属性改成可执行状态就可以绕过DEP了. 首先看一下Vir ...

  4. [课程设计]Scrum 1.7 多鱼点餐系统开发进度

    [课程设计]Scrum 1.7 多鱼点餐系统开发进度(点餐菜式内容添加及美化) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题 ...

  5. 利用web工具splinter模拟登陆做自动签到

    首先,我需要的工具和组件有: Chrome浏览器 浏览器驱动ChromeDriver Python 3.5 Web应用测试工具Splinter 代码部分: from splinter import B ...

  6. [MySQL5.6 新特性] 全局事务标示符(GTID)

    GTID的全称为 global transaction identifier  , 可以翻译为全局事务标示符,GTID在原始master上的事务提交时被创建.GTID需要在全局的主-备拓扑结构中保持唯 ...

  7. [linux] linux shell 将解析完毕的文件备份至其他目录

    #!/bin/bash # #将解析完毕的日志备份到别的目录 # #日志目录 rjPath=`ls /home/bgftp/orj` #当前时间戳 cur=`date +%s` #一小时 chtm=3 ...

  8. 在ASP.NET Web API中使用OData

    http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...

  9. 重装系统后,delphi7打开报错

    delphi7运行不正常的提示unable to rename'c:\program files\Borland\delphi7\Bin\delphi32.$$$'to'c:\program file ...

  10. vmware12无法打开内核设备“\\.\Global\vmx86”

    vmware12 无法打开内核设备"\\.\Global\vmx86": 系统找不到指定的文件.你想要在安装 VMware Workstation 前重启吗? 打开vmware12 ...