Guest User

Untitled

a guest
Sep 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #ifndef LINKED_LIST_NODE_HPP
  2. #define LINKED_LIST_NODE_HPP
  3.  
  4. template <typename T>
  5. class Node{
  6. public:
  7. T data;
  8. Node* next;
  9. Node();
  10. Node(T);
  11. Node(const Node&);
  12. ~Node()
  13.  
  14. };
  15.  
  16. Node<T>::Node(){}
  17. Node<T>:: Node(const T data): data(data), next(nullptr){}
  18. Node<T>::Node(const Node& source){
  19. Node<T>* copy = new Node<T>(source.data))
  20. copy.next = nullptr;
  21. }
  22. ~Node(){
  23. delete this;
  24. }
  25.  
  26.  
  27. #endif //LINKED_LIST_NODE_HPP
  28.  
  29. #ifndef LINKED_LIST_SINGLYLINKEDLIST_HPP
  30. #define LINKED_LIST_SINGLYLINKEDLIST_HPP
  31.  
  32. #include <iostream>
  33. #include "node.hpp"
  34.  
  35. template <typename T>
  36. class SinglyLinkedList {
  37.  
  38. private:
  39. Node<T>* head;
  40. std::size_t count;
  41.  
  42. public:
  43. SinglyLinkedList();
  44. SinglyLinkedList(const SinglyLinkedList& source);
  45. SinglyLinkedList& operator=(const SinglyLinkedList& source);
  46. ~SinglyLinkedList();
  47. void insert(T);
  48. void remove(T);
  49. bool isEmpty();
  50. int length();
  51. void print();
  52.  
  53. };
  54.  
  55. template <typename T>
  56. SinglyLinkedList<T>::SinglyLinkedList() : head(nullptr), count(0){}
  57.  
  58. SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& source){
  59. //This might not be correct
  60. Node<T> new_head = Node(source.head);
  61.  
  62. while(head != nullptr){
  63. Node<T> prev = new_head;
  64. head = head.next;
  65. copy_head = Node(head);
  66. prev.next = new_head
  67. }
  68.  
  69. }
  70.  
  71. SinglyLinkedList<T>::SinglyLinkedList& operator=(const SinglyLinkedList& source){
  72. //not sure how to implment this.
  73. }
  74.  
  75. template <typename T>
  76. SinglyLinkedList<T>::~SinglyLinkedList() {
  77. if(!isEmpty()){
  78. Node<T>* temp = head;
  79. Node<T>* prev = nullptr;
  80. while(temp->next != nullptr){
  81. prev = temp;
  82. temp = temp->next;
  83. delete prev;
  84. }
  85. delete temp;
  86. }
  87. }
  88.  
  89. template <typename T>
  90. bool SinglyLinkedList<T>::isEmpty() {
  91. return head == nullptr;
  92. }
  93.  
  94. template <typename T>
  95. void SinglyLinkedList<T>::insert(T item) {
  96. Node<T>* p = new Node<T>(item);
  97. p->next = head;
  98. head = p;
  99. count += 1;
  100.  
  101. }
  102.  
  103. template <typename T>
  104. void SinglyLinkedList<T>::remove(T item) {
  105. bool present = false;
  106. if (head->data == item){
  107. Node<T>* temp = head;
  108. head = head->next;
  109. delete(temp);
  110. count -= 1;
  111. return;
  112. }
  113. Node<T>* temp = head;
  114. while (temp->next != nullptr){
  115. if (temp->next->data == item){
  116. Node<T>* removable = temp->next;
  117. temp->next = temp->next->next;
  118. delete(removable);
  119. present = true;
  120. count -= 1;
  121. break;
  122. } else{
  123. temp = temp->next;
  124. }
  125. }
  126. if(!present){
  127. throw std::invalid_argument("item not present in list");
  128. }
  129. }
  130.  
  131. template <typename T>
  132. int SinglyLinkedList<T>::length() {
  133. return count;
  134. }
  135.  
  136. template <typename T>
  137. void SinglyLinkedList<T>::print() {
  138. if(isEmpty()){
  139. throw std::invalid_argument("Can't print an empty list!");
  140. }
  141. Node<T>* temp = head;
  142. while(temp != nullptr){
  143. if(temp->next != nullptr){
  144. std::cout<<temp->data;
  145. std::cout<<"->";
  146. }else{
  147. std::cout<<temp->data;
  148. }
  149. temp = temp->next;
  150. }
  151. std::cout<<std::endl;
  152.  
  153. }
  154.  
  155. #endif //LINKED_LIST_SINGLYLINKEDLIST_HPP
Add Comment
Please, Sign In to add comment