Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct row
  7. {
  8. string key;
  9. string value;
  10. row* next;
  11. };
  12.  
  13. class table
  14. {
  15. private:
  16. row* begin;
  17. public:
  18. table()
  19. {
  20. begin = nullptr;
  21. }
  22. void addRow(string k, string val)
  23. {
  24. row* elemToAdd = new row;
  25. elemToAdd->key = k;
  26. elemToAdd->value = val;
  27. elemToAdd->next = begin;
  28. begin = elemToAdd;
  29. }
  30. void changeRow(string k, string val)
  31. {
  32. row* rowToCheck = begin;
  33. while (rowToCheck != nullptr)
  34. {
  35. if (rowToCheck->key == k)
  36. {
  37. rowToCheck->value = val;
  38. break;
  39. }
  40. rowToCheck = rowToCheck->next;
  41. }
  42. }
  43. void deleteRow(string k)
  44. {
  45. if (begin == nullptr)
  46. {
  47. cout << "No elements!\n";
  48. return;
  49. }
  50. row* rowToDelete = begin;
  51. if (rowToDelete->key == k)
  52. {
  53. begin = begin->next;
  54. delete rowToDelete;
  55. return;
  56. }
  57. while (rowToDelete->next != nullptr)
  58. {
  59. if (rowToDelete->next->key == k)
  60. {
  61. rowToDelete->next = rowToDelete->next->next;
  62. delete rowToDelete->next;
  63. return;
  64. }
  65. rowToDelete = rowToDelete->next;
  66. }
  67. cout << "No such an element!\n";
  68. }
  69. string findRow(string k)
  70. {
  71. row* rowForCheck = begin;
  72. while (rowForCheck != nullptr)
  73. {
  74. if (rowForCheck->key == k)
  75. {
  76. return rowForCheck->value;
  77. }
  78. rowForCheck = rowForCheck->next;
  79. }
  80. return "Row wasn't found!\n";
  81. }
  82. void doIteration(void(*func)(row*))
  83. {
  84. row* el = begin;
  85. while (el != nullptr)
  86. {
  87. (*func)(el);
  88. el = el->next;
  89. }
  90. }
  91. ~table()
  92. {
  93. if (begin == nullptr)
  94. {
  95. return;
  96. }
  97. row* t = begin;
  98. while (t != nullptr)
  99. {
  100. begin = begin->next;
  101. delete t;
  102. t = begin;
  103. }
  104. }
  105. };
  106.  
  107. void iterate(row* r)
  108. {
  109. r->value += "val";
  110. }
  111.  
  112. bool testIterate()
  113. {
  114. bool testResult = true;
  115. table tab;
  116. tab.addRow("key1", "value1");
  117. tab.addRow("key2", "value2");
  118. tab.addRow("key3", "value3");
  119. tab.doIteration(iterate);
  120. testResult &= (tab.findRow("key1") == "value1val");
  121. testResult &= (tab.findRow("key2") == "value2val");
  122. testResult &= (tab.findRow("key3") == "value3val");
  123. return testResult;
  124. }
  125.  
  126. bool testAddFind()
  127. {
  128. table tab;
  129. bool testResult = true;
  130. tab.addRow("first key", "first value");
  131. tab.addRow("second key", "second value");
  132. testResult &= (tab.findRow("first key") == "first value");
  133. testResult &= (tab.findRow("second key") == "second value");
  134. testResult &= (tab.findRow("third key") == "Row wasn't found!\n");
  135. return testResult;
  136. }
  137.  
  138. bool testChange()
  139. {
  140. table tab;
  141. bool testResult = true;
  142. tab.addRow("first key", "first value");
  143. tab.addRow("second key", "second value");
  144. tab.changeRow("first key", "not a first value");
  145. testResult &= (tab.findRow("first key") == "not a first value");
  146. return testResult;
  147. }
  148.  
  149. bool testDelete()
  150. {
  151. table tab;
  152. bool testResult = true;
  153. tab.addRow("first key", "first value");
  154. tab.addRow("second key", "second value");
  155. tab.deleteRow("first key");
  156. tab.deleteRow("second key");
  157. testResult &= (tab.findRow("first key") == "Row wasn't found!\n");
  158. testResult &= (tab.findRow("second key") == "Row wasn't found!\n");
  159. return testResult;
  160. }
  161.  
  162. int main()
  163. {
  164. cout << "Table is adding and finding: " << boolalpha << testAddFind() << endl;
  165. cout << "Table is changing: " << testChange() << endl;
  166. cout << "Table is deleting: " << testDelete() << endl;
  167. cout << "Iterating... " << testIterate() << endl;
  168.  
  169. system("pause");
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement