Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. ///Linked list///
  2.  
  3. #include <iostream>
  4. #include <stdexcept>
  5. using namespace std;
  6.  
  7. struct Node {
  8. int data;
  9. Node* next = nullptr;
  10.  
  11. Node(int data, Node* next = nullptr) {
  12. this->data = data;
  13. this->next = next;
  14. }
  15. };
  16.  
  17. class SinglyLL {
  18. private:
  19. Node* head = nullptr;
  20. Node* tail = nullptr;
  21. public:
  22. SinglyLL() = default;
  23.  
  24. void freeMemory() {
  25. Node* i = head;
  26. while(i != nullptr) {
  27. Node* next = i->next;
  28. delete i;
  29. i = next;
  30. }
  31.  
  32. head = nullptr;
  33. tail = nullptr;
  34. }
  35.  
  36. ~SinglyLL() {
  37. freeMemory();
  38. }
  39.  
  40. void copyLinkedList(const SinglyLL& rhs) {
  41. Node* i = rhs.head;
  42. while(i != nullptr) {
  43. push_back(i->data);
  44. i = i->next;
  45. }
  46. }
  47.  
  48. SinglyLL& operator=(const SinglyLL& rhs) {
  49. if (this != &rhs) {
  50. freeMemory();
  51. copyLinkedList(rhs);
  52. }
  53. return *this;
  54. }
  55.  
  56. SinglyLL(const SinglyLL& rhs) {
  57. copyLinkedList(rhs);
  58. }
  59.  
  60. void push_front(int number) {
  61. Node* newHead = new Node(number, head);
  62. head = newHead;
  63. if (tail == nullptr) {
  64. tail = head;
  65. }
  66. }
  67.  
  68. void push_back(int number) {
  69. if (head == nullptr) {
  70. head = new Node(number);
  71. tail = head;
  72. } else {
  73. tail->next = new Node(number);
  74. tail = tail->next;
  75. }
  76. }
  77.  
  78. void print() const {
  79. Node* i = head;
  80. while (i != nullptr) {
  81. cout << i->data << " ";
  82. i = i->next;
  83. }
  84. cout << "\n";
  85. }
  86.  
  87. int peek_front() {
  88. if (head != nullptr) {
  89. return head->data;
  90. } else {
  91. throw std::out_of_range("List is empty, cannot peek_front()");
  92. }
  93. }
  94.  
  95. int peek_back() {
  96. if (tail != nullptr) {
  97. return tail->data;
  98. } else {
  99. throw std::out_of_range("List is empty, cannot peek_back()");
  100. }
  101. }
  102.  
  103. void pop_front() {
  104. if (head != nullptr) {
  105. Node* oldHead = head;
  106. if (tail == head) {
  107. tail = nullptr;
  108. }
  109. head = head->next;
  110. delete oldHead;
  111. } else {
  112. throw std::out_of_range("List is empty, cannot pop_front()");
  113. }
  114. }
  115.  
  116. void pop_back() {
  117. if (head != nullptr) {
  118. if (head == tail) {
  119. delete head;
  120. head = nullptr;
  121. tail = nullptr;
  122. }
  123.  
  124. // Find the node pointing to the tail
  125. Node* i = head;
  126. while (i-> next != nullptr && i->next != tail) {
  127. i = i->next;
  128. }
  129.  
  130. // Delete the tail and point tail to the current node (i)
  131. delete i->next;
  132. i->next = nullptr;
  133. tail = i;
  134. } else {
  135. throw std::out_of_range("List is empty, cannot pop_front()");
  136. }
  137. }
  138.  
  139. void insert(int number, int position) {
  140. if (position == 0) {
  141. push_front(number);
  142. return;
  143. }
  144.  
  145. if (head == nullptr) {
  146. throw std::out_of_range("Invalid insert index, list is empty");
  147. }
  148.  
  149. Node* current = head;
  150. Node* previous = nullptr;
  151. for (int i = 0; i < position; i++) {
  152. if (current != nullptr) {
  153. previous = current;
  154. current = current->next;
  155. } else {
  156. throw std::out_of_range("Cannot insert in invalid position");
  157. }
  158. }
  159.  
  160. previous->next = new Node(number, current);
  161. if (tail == previous) {
  162. tail = previous->next;
  163. }
  164. }
  165.  
  166. int get(int position) {
  167. if (head == nullptr) {
  168. throw std::out_of_range("List is empty, cannot get from it");
  169. }
  170.  
  171. Node* current = head;
  172. for (int i = 0; i < position; i++) {
  173. if (current->next != nullptr) {
  174. current = current->next;
  175. } else {
  176. throw std::out_of_range("Cannot get from list, no such position");
  177. }
  178. }
  179.  
  180. return current->data;
  181. }
  182.  
  183. void remove(int position) {
  184. if (position == 0) {
  185. pop_front();
  186. return;
  187. }
  188.  
  189. if (head == nullptr) {
  190. throw std::out_of_range("Invalid remove index, list is empty");
  191. }
  192.  
  193. Node* current = head;
  194. Node* previous = nullptr;
  195. for (int i = 0; i < position; i++) {
  196. if (current->next != nullptr) {
  197. previous = current;
  198. current = current->next;
  199. } else {
  200. throw std::out_of_range("Cannot insert in invalid position");
  201. }
  202. }
  203.  
  204. previous->next = current->next;
  205. if (tail == current) {
  206. tail = previous;
  207. }
  208. delete current;
  209. }
  210.  
  211. };
  212.  
  213. int main() {
  214. SinglyLL l;
  215. l.insert(0, 0);
  216. l.insert(1, 1);
  217. l.insert(2, 2);
  218. l.insert(3, 3);
  219. l.print();
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement