Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. #ifndef BLINKEDLIST_H
  2. #define BLINKEDLIST_H
  3.  
  4. #include <memory>
  5.  
  6. struct Node
  7. {
  8. int key;
  9. Node* next;
  10. };
  11.  
  12. class BLinkedlist
  13. {
  14. public:
  15. BLinkedlist();
  16. void push_back(int value);
  17. void push_front(int value);
  18. void insert(int idx, int value);
  19. int pop_front();
  20. int pop_back();
  21. void erase(int idx);
  22. int remove_value(int value); //removes the first item in the list with this value
  23. void display();
  24. bool empty();
  25. int value_at(int idx);
  26. int value_n_from_end(int idx);
  27. int front();
  28. int back();
  29. void reverse();
  30. private:
  31. int size;
  32. Node* head;
  33. Node* tail;
  34. };
  35.  
  36. #endif // BLINKEDLIST_H
  37.  
  38. #include "blinkedlist.h"
  39. #include <stdexcept>
  40. #include <iostream>
  41.  
  42. BLinkedlist::BLinkedlist():size(0), head(nullptr), tail(nullptr)
  43. {
  44.  
  45. }
  46.  
  47. void BLinkedlist::push_back(int value)
  48. {
  49. Node* newNode = new Node();
  50. newNode->key = value;
  51. if(head != nullptr)
  52. {
  53. tail->next = newNode;
  54. }
  55. else
  56. {
  57. head = newNode;
  58. }
  59. tail = newNode;
  60. newNode->next = nullptr;
  61. ++size;
  62. }
  63.  
  64. void BLinkedlist::push_front(int value)
  65. {
  66. Node* newNode = new Node();
  67. newNode->key = value;
  68. if(head != nullptr)
  69. {
  70. newNode->next = head;
  71. }
  72. else
  73. {
  74. newNode->next = nullptr;
  75. tail = newNode;
  76. }
  77. head = newNode;
  78. ++size;
  79. }
  80.  
  81. void BLinkedlist::insert(int idx, int value)
  82. {
  83. if(idx < 0 || idx > size)
  84. {
  85. throw std::out_of_range("Index is out of range!");
  86. }
  87. else
  88. {
  89. Node* newNode = new Node();
  90. newNode->key = value;
  91.  
  92. if(idx == 0)
  93. {
  94. push_front(value);
  95. }
  96. else if(idx == size)
  97. {
  98. push_back(value);
  99. }
  100. else
  101. {
  102. Node* curr = head;
  103. int i = idx-1;
  104. while(i > 0)
  105. {
  106. curr = curr->next;
  107. --i;
  108. }
  109. newNode->next = curr->next;
  110. curr->next = newNode;
  111. ++size;
  112. }
  113.  
  114. }
  115.  
  116. }
  117.  
  118. int BLinkedlist::pop_front()
  119. {
  120. if(head != nullptr && size >= 2)
  121. {
  122. int val = head->key;
  123. head = head->next;
  124. --size;
  125. return val;
  126. }
  127. else if(head != nullptr && size == 1)
  128. {
  129. int val = head->key;
  130. head = nullptr;
  131. tail = nullptr;
  132. --size;
  133. return val;
  134. }
  135. else
  136. {
  137. throw std::logic_error("List is already empty!");
  138. }
  139. }
  140.  
  141. int BLinkedlist::pop_back()
  142. {
  143. if(head != nullptr && size > 2)
  144. {
  145. int val;
  146. Node* curr = head;
  147. while(curr->next->next != nullptr)
  148. {
  149. curr = curr->next;
  150. }
  151. val = curr->next->key;
  152. curr->next = nullptr;
  153. tail = curr;
  154. --size;
  155. return val;
  156. }
  157. else if(head != nullptr && size == 2)
  158. {
  159. int val = head->next->key;
  160. tail = head;
  161. --size;
  162. return val;
  163. }
  164. else if(head != nullptr && size == 1)
  165. {
  166. int val = head->key;
  167. head = nullptr;
  168. tail = nullptr;
  169. --size;
  170. return val;
  171. }
  172. else
  173. {
  174. throw std::logic_error("List is already empty!");
  175. }
  176. }
  177.  
  178. void BLinkedlist::erase(int idx)
  179. {
  180. if(idx < 0 || idx > size)
  181. {
  182. throw std::out_of_range("Index is out of range!");
  183. }
  184. else
  185. {
  186. if(idx == 0)
  187. {
  188. pop_front();
  189. }
  190. else if(idx == size)
  191. {
  192. pop_back();
  193. }
  194. else
  195. {
  196. Node* curr = head;
  197. int i = idx-1;
  198. while(i > 0)
  199. {
  200. curr = curr->next;
  201. --i;
  202. }
  203. curr->next = curr->next->next;
  204. --size;
  205. }
  206. }
  207. }
  208.  
  209. int BLinkedlist::remove_value(int value)
  210. {
  211. Node* curr = head;
  212. int i = 0;
  213. while(curr != nullptr)
  214. {
  215. if(curr->key == value)
  216. {
  217. erase(i);
  218. return i;
  219. }
  220. curr = curr->next;
  221. ++i;
  222. }
  223. return -1;
  224. }
  225.  
  226. void BLinkedlist::display()
  227. {
  228. Node* curr = head;
  229. while(curr != nullptr)
  230. {
  231. std::cout << curr->key << std::endl;
  232. curr = curr->next;
  233. }
  234. }
  235.  
  236. bool BLinkedlist::empty()
  237. {
  238. return size == 0;
  239. }
  240.  
  241. int BLinkedlist::value_at(int idx)
  242. {
  243. Node* temp = head;
  244. if(idx < 0 || idx > size)
  245. {
  246. throw std::out_of_range("Index is out of range!");
  247. }
  248. else
  249. {
  250. int i = idx;
  251. while(i != 0 && temp != nullptr)
  252. {
  253. temp = temp->next;
  254. --i;
  255. }
  256. return temp->key;
  257. }
  258. }
  259.  
  260. int BLinkedlist::value_n_from_end(int idx)
  261. {
  262. if(idx < 0 || idx > size)
  263. {
  264. throw std::out_of_range("Index is out of range!");
  265. }
  266. else
  267. {
  268. int forwardIdx = size - idx;
  269. if(idx == 0)
  270. return tail->key;
  271. Node* curr = head;
  272. while(forwardIdx > 0)
  273. {
  274. curr = curr->next;
  275. --forwardIdx;
  276. }
  277. return curr->key;
  278. }
  279. }
  280.  
  281. int BLinkedlist::front()
  282. {
  283. return head->key;
  284. }
  285.  
  286. int BLinkedlist::back()
  287. {
  288. return tail->key;
  289. }
  290.  
  291. void BLinkedlist::reverse()
  292. {
  293. Node* next = nullptr;
  294. Node* prev = nullptr;
  295. Node* curr = head;
  296.  
  297. while(curr != nullptr)
  298. {
  299. next = curr->next;
  300. curr->next = prev;
  301. prev = curr;
  302. curr = next;
  303. }
  304. head = prev;
  305. }
  306.  
  307. #include <iostream>
  308. #include "blinkedlist.h"
  309.  
  310. int main()
  311. {
  312. BLinkedlist list;
  313.  
  314. list.push_back(1);
  315. list.push_back(2);
  316. list.push_back(3);
  317. list.push_back(4);
  318. list.push_back(5);
  319. list.push_back(6);
  320.  
  321. list.display();
  322.  
  323. std::cout << "----------------------" << std::endl;
  324.  
  325. list.reverse();
  326.  
  327. list.display();
  328.  
  329. return 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement