fork download
  1. #include <iostream>
  2.  
  3. struct Snode
  4. {
  5. char data;
  6. int count;
  7. Snode *next = nullptr;
  8. Snode(char a, int c = 1) : data(a), count(c) {}
  9. };
  10.  
  11. class set
  12. {
  13. private:
  14. Snode *head = nullptr;
  15. Snode *tail = nullptr;
  16.  
  17. void append(char value, int count)
  18. {
  19. Snode *temp = new Snode(value, count);
  20.  
  21. if (!head)
  22. head = temp;
  23.  
  24. if (tail)
  25. tail->next = temp;
  26. tail = temp;
  27. }
  28.  
  29. void remove(Snode *node, Snode *previous)
  30. {
  31. if (previous)
  32. previous->next = node->next;
  33.  
  34. if (head == node)
  35. head = node->next;
  36.  
  37. if (tail == node)
  38. tail = previous;
  39.  
  40. delete node;
  41. }
  42.  
  43. void swap(set &other)
  44. {
  45. Snode *ptr = head;
  46. head = other.head;
  47. other.head = ptr;
  48.  
  49. ptr = tail;
  50. tail = other.tail;
  51. other.tail = ptr;
  52. }
  53.  
  54. public:
  55. set() = default;
  56.  
  57. set(const set &src)
  58. {
  59. Snode *temp = src.head;
  60. while (temp)
  61. {
  62. append(temp->data, temp->count);
  63. temp = temp->next;
  64. }
  65. }
  66.  
  67. set(set &&src)
  68. {
  69. src.swap(*this);
  70. }
  71.  
  72. ~set()
  73. {
  74. Snode *temp = head;
  75. while (temp)
  76. {
  77. Snode *next = temp->next;
  78. delete temp;
  79. temp = next;
  80. }
  81. }
  82.  
  83. set& operator=(const set &rhs)
  84. {
  85. if (&rhs != this)
  86. {
  87. set temp(rhs);
  88. temp.swap(*this);
  89. }
  90. return *this;
  91. }
  92.  
  93. set& operator=(set &&rhs)
  94. {
  95. rhs.swap(*this);
  96. return *this;
  97. }
  98.  
  99. bool isAvailable(char value)
  100. {
  101. return (find(value) != nullptr);
  102. }
  103.  
  104. Snode* find(char value, Snode **previous = nullptr)
  105. {
  106. if (previous)
  107. *previous = nullptr;
  108.  
  109. Snode *temp = head;
  110. Snode *prev = nullptr;
  111.  
  112. while (temp)
  113. {
  114. if (temp->data == value)
  115. {
  116. if (previous)
  117. *previous = prev;
  118. return temp;
  119. }
  120.  
  121. temp = temp->next;
  122. }
  123.  
  124. return nullptr;
  125. }
  126.  
  127. bool isFirst(char value)
  128. {
  129. return ((head) && (head->data == value));
  130. }
  131.  
  132. bool isLast(char value)
  133. {
  134. return ((tail) && (tail->data == value));
  135. }
  136.  
  137. void display()
  138. {
  139. Snode *temp = head;
  140. while (temp)
  141. {
  142. std::cout << temp->data << " " << temp->count << std::endl;
  143. temp = temp->next;
  144. }
  145. }
  146.  
  147. void insert(char value)
  148. {
  149. Snode *temp = find(value);
  150. if (temp)
  151. temp->count += 1;
  152. else
  153. append(value, 1);
  154. }
  155.  
  156. int count(char value)
  157. {
  158. Snode *temp = find(value);
  159. return (temp) ? temp->count : 0;
  160. }
  161.  
  162. void deleteFirst()
  163. {
  164. if (head)
  165. remove(head, nullptr);
  166. }
  167.  
  168. void deleteLast()
  169. {
  170. if (head)
  171. {
  172. Snode *last = head;
  173. Snode *previous = nullptr;
  174.  
  175. while (last->next)
  176. {
  177. previous = last;
  178. last = last->next;
  179. }
  180.  
  181. remove(last, previous);
  182. }
  183. }
  184.  
  185. void remove(char value)
  186. {
  187. Snode *previous;
  188. Snode *temp = find(value, &previous);
  189. if (temp)
  190. {
  191. if (temp->count > 1)
  192. temp->count -= 1;
  193. else
  194. remove(temp, previous);
  195. }
  196. }
  197. };
  198.  
  199. int main()
  200. {
  201. //defining a mySet as a "set" type
  202. set mySet;
  203.  
  204. //adding values to create nodes
  205. mySet.insert('c');
  206. mySet.insert('a');
  207. mySet.insert('a');
  208. mySet.insert('c');
  209. mySet.insert('c');
  210.  
  211. set myCopiedSet = mySet; // make a copy of the list
  212.  
  213. //adding more values to create nodes
  214. myCopiedSet.insert('a');
  215. myCopiedSet.insert('b');
  216. myCopiedSet.insert('b');
  217. myCopiedSet.insert('c');
  218.  
  219. // another test
  220. set myTestSet;
  221. myTestSet.insert('c');
  222. myTestSet.insert('c');
  223. myTestSet.insert('j');
  224. myTestSet.insert('j');
  225. myTestSet.insert('j');
  226. myTestSet.insert('r');
  227.  
  228. //displaying nodes through "value count" format
  229. std::cout << "original:" << std::endl;
  230. mySet.display();
  231. std::cout << "copy:" << std::endl;
  232. myCopiedSet.display();
  233. std::cout << "test:" << std::endl;
  234. myTestSet.display();
  235.  
  236. return 0;
  237. }
  238.  
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
original:
c 3
a 2
copy:
c 4
a 3
b 2
test:
c 2
j 3
r 1