Advertisement
GerONSo

List

Oct 17th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. #pragma once
  2. #include <cstddef>
  3. #include <iostream>
  4.  
  5.  
  6. namespace task {
  7.  
  8. struct node {
  9. node *left;
  10. node *right;
  11. int *value;
  12.  
  13. node() {
  14. left = nullptr;
  15. right = nullptr;
  16. value = nullptr;
  17. }
  18.  
  19. node(int _value) {
  20. left = nullptr;
  21. right = nullptr;
  22. value = new int(_value);
  23. }
  24.  
  25. ~node() {
  26. delete value;
  27. }
  28. };
  29.  
  30.  
  31. class list {
  32.  
  33. public:
  34.  
  35. list() {
  36. begin = nullptr;
  37. end = nullptr;
  38. };
  39.  
  40. list(int count, const int& value = int()) {
  41.  
  42. node *tmpList[count];
  43. for(int i = count - 1; i >= 0; i--) {
  44. tmpList[i] = new node(value);
  45. if(i < count - 1) {
  46. tmpList[i]->right = tmpList[i + 1];
  47. }
  48. }
  49. for(int i = 1; i < count; i++) {
  50. tmpList[i]->left = tmpList[i - 1];
  51. }
  52. begin = tmpList[0];
  53. end = tmpList[count - 1];
  54. }
  55.  
  56. list(const list &other) {
  57. if(other.begin == nullptr) {
  58. begin = nullptr;
  59. end = nullptr;
  60. return;
  61. }
  62. node *cur = other.begin;
  63. const int otherSize = other.size();
  64. node *tmpList[otherSize];
  65. int curSize = 0;
  66. while(cur != nullptr) {
  67. tmpList[curSize] = new node(*cur->value);
  68. cur = cur->right;
  69. curSize++;
  70. }
  71. for(int i = 0; i < otherSize; i++) {
  72. tmpList[i]->right = (i < otherSize - 1) ? tmpList[i + 1] : nullptr;
  73. tmpList[i]->left = (i > 0) ? tmpList[i - 1] : nullptr;
  74. }
  75.  
  76.  
  77. begin = tmpList[0];
  78. end = tmpList[otherSize - 1];
  79. // node *current = begin;
  80. // while(current != nullptr) {
  81. // std::cout << current->value << " ";
  82. // current = current->right;
  83. // }
  84. // std::cout << '\n';
  85. }
  86.  
  87. ~list() {
  88. node *current = begin;
  89. while(current != nullptr) {
  90. node *next = current->right;
  91. delete current;
  92. current = next;
  93. }
  94. }
  95.  
  96. list& operator=(const list& other) {
  97. node *current = begin;
  98. while(current != nullptr) {
  99. node *next = current->right;
  100. delete current;
  101. current = next;
  102. }
  103. begin = nullptr;
  104. end = nullptr;
  105. if(other.begin == nullptr) {
  106. return *this;
  107. }
  108. node *cur = other.begin;
  109. const int otherSize = other.size();
  110. node *tmpList[otherSize];
  111. int curSize = 0;
  112. while(cur != nullptr) {
  113. tmpList[curSize] = new node(*cur->value);
  114. cur = cur->right;
  115. curSize++;
  116. }
  117. for(int i = 0; i < otherSize; i++) {
  118. tmpList[i]->right = (i < otherSize - 1) ? tmpList[i + 1] : nullptr;
  119. tmpList[i]->left = (i > 0) ? tmpList[i - 1] : nullptr;
  120. }
  121.  
  122.  
  123. begin = tmpList[0];
  124. end = tmpList[otherSize - 1];
  125. return *this;
  126. }
  127.  
  128.  
  129. int& front() {
  130. return *begin->value;
  131. }
  132.  
  133. const int& front() const {
  134. return *begin->value;
  135. }
  136.  
  137. int& back() {
  138. return *end->value;
  139. }
  140.  
  141. const int& back() const {
  142. return *end->value;
  143. }
  144.  
  145.  
  146. bool empty() const {
  147. return (begin == nullptr);
  148. }
  149.  
  150. int size() const {
  151. node *current = begin;
  152. int size = 0;
  153. while(current != nullptr) {
  154. size++;
  155. current = current->right;
  156. }
  157. return size;
  158. }
  159.  
  160. void print() const {
  161. node *current = begin;
  162. while(current != nullptr) {
  163. std::cout << *current->value << ' ';
  164. current = current->right;
  165. }
  166. std::cout << '\n';
  167. }
  168.  
  169. void clear() {
  170. node *current = begin;
  171. while(current != nullptr) {
  172. node *next = current->right;
  173. delete current;
  174. current = next;
  175. }
  176. begin = nullptr;
  177. end = nullptr;
  178. }
  179.  
  180. void push_back(const int& value) {
  181. node *tmp = new node(value);
  182. if(begin == nullptr) {
  183. begin = tmp;
  184. end = tmp;
  185. return;
  186. }
  187. tmp->left = end;
  188. end->right = tmp;
  189. end = tmp;
  190. }
  191.  
  192. void pop_back() {
  193. if(end->left != nullptr) {
  194. end = end->left;
  195. delete end->right;
  196. end->right = nullptr;
  197. }
  198. else {
  199. delete begin;
  200. begin = nullptr;
  201. end = nullptr;
  202. }
  203. }
  204.  
  205. void push_front(const int& value) {
  206. node *tmp = new node(value);
  207. if(begin == nullptr) {
  208. begin = tmp;
  209. end = tmp;
  210. return;
  211. }
  212. tmp->right = begin;
  213. begin->left = tmp;
  214. begin = tmp;
  215.  
  216. }
  217.  
  218. void pop_front() {
  219. if(begin->right != nullptr) {
  220. begin = begin->right;
  221. delete begin->left;
  222. begin->left = nullptr;
  223. }
  224. else {
  225. delete begin;
  226. begin = nullptr;
  227. end = nullptr;
  228. }
  229. }
  230.  
  231. void resize(int count) {
  232. int sz = size();
  233. while(count < sz) {
  234. pop_back();
  235. sz--;
  236. }
  237. while(sz < count) {
  238. push_back(0);
  239. sz++;
  240. }
  241. }
  242.  
  243. void swap(list& other) {
  244. std::swap(begin, other.begin);
  245. std::swap(end, other.end);
  246. }
  247.  
  248. void remove(const int &value) {
  249. int tmpValue = value;
  250. node *cur = begin;
  251. if(cur == nullptr) {
  252. begin = nullptr;
  253. end = nullptr;
  254. return;
  255. }
  256. cur = cur->right;
  257. while(cur != end && cur != nullptr) {
  258. node *next = cur->right;
  259. if(*(cur->value) == tmpValue) {
  260. cur->left->right = cur->right;
  261. cur->right->left = cur->left;
  262. delete cur;
  263. cur = next;
  264. }
  265. else {
  266. cur = next;
  267. }
  268.  
  269. }
  270. if(begin != nullptr && *(begin->value) == tmpValue) {
  271. if(begin->right != nullptr) {
  272. begin = begin->right;
  273. delete begin->left;
  274. begin->left = nullptr;
  275. }
  276. else {
  277. delete begin;
  278. begin = nullptr;
  279. end = nullptr;
  280. }
  281. }
  282. if(end != nullptr && *(end->value) == tmpValue) {
  283. if(end->left != nullptr) {
  284. end = end->left;
  285. delete end->right;
  286. end->right = nullptr;
  287. }
  288. else {
  289. delete begin;
  290. begin = nullptr;
  291. end = nullptr;
  292. }
  293. }
  294. }
  295.  
  296. void unique() {
  297. node *cur = begin;
  298. while(cur != nullptr) {
  299. node *next = cur->right;
  300. if(cur->left != nullptr && *(cur->left->value) == *(cur->value)) {
  301. cur->left->right = cur->right;
  302. if(cur->right != nullptr) {
  303. cur->right->left = cur->left;
  304. }
  305. delete cur;
  306. cur = next;
  307. }
  308. else {
  309. cur = next;
  310. }
  311.  
  312. }
  313. }
  314.  
  315. void sort() {
  316. node *current = begin;
  317. while(current != nullptr) {
  318. node *current2 = current;
  319. while(current2 != nullptr) {
  320. if(*(current->value) > *(current2->value)) {
  321. swap(current, current2);
  322. }
  323. current2 = current2->right;
  324. }
  325. current = current->right;
  326. }
  327. }
  328.  
  329. // Your code goes here?..
  330.  
  331. private:
  332.  
  333. node *begin = nullptr;
  334. node *end = nullptr;
  335.  
  336. void swap(node *&a, node *&b) {
  337. std::swap(a->value, b->value);
  338. }
  339. };
  340.  
  341. } // namespace task
  342.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement