Advertisement
Andrey_ZoZ

Untitled

Aug 1st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename ElemT>
  4. class ListFw
  5. {
  6.     class Node
  7.     {
  8.     public:
  9.         ElemT data;
  10.         Node* next;
  11.         Node(const ElemT& dataP, Node* nextP = nullptr)
  12.             :data{ dataP }, next{ nextP }{};
  13.     };
  14.  
  15.     Node* head;
  16.     Node* tail;
  17.     size_t size;
  18. public:
  19.     ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
  20.     ElemT& front() { return head->data; };
  21.     const ElemT& front()const { return head->data; };
  22.     ElemT& back() { return tail->data; };
  23.     const ElemT& back()const { return tail->data; };
  24.     const size_t getSize() const { return size; };
  25.     bool empty() const { return size == 0; };
  26.     void push_back(const ElemT& data);
  27.     void push_front(const ElemT& data);
  28.     bool pop_back();
  29.     bool pop_front();
  30.     void clear();
  31.     ListFw clone();
  32.     ListFw operator+(ListFw& object);
  33.     ListFw operator*(ListFw& object);
  34.     void operator=(ListFw& object)
  35.     {
  36.         clear();
  37.         for (Node* curr{ object.head }; curr != nullptr; curr = curr->next) {
  38.             push_front(curr->data);
  39.         }
  40.     }
  41.     void operator=(ListFw&& object)
  42.     {
  43.         clear();
  44.         size = object.size;
  45.         head = object.head;
  46.         tail = object.tail;
  47.         object.head = nullptr;
  48.         object.tail = nullptr;
  49.     }
  50.     ListFw(ListFw& object)
  51.     {
  52.         size = object.size;
  53.         for (Node* curr{ object.head }; curr != nullptr; curr = curr->next) {
  54.             push_front(curr->data);
  55.         }
  56.     }
  57.     ListFw(ListFw&& object)
  58.     {
  59.         size = object.size;
  60.         head = object.head;
  61.         tail = object.tail;
  62.         object.head = nullptr;
  63.         object.tail = nullptr;
  64.     }
  65.     ~ListFw();
  66. };
  67.  
  68. template <typename ElemT>
  69. void ListFw<ElemT>::push_back(const ElemT& data)
  70. {
  71.     if (!head)
  72.     {
  73.         head = new Node{ data };
  74.         tail = head;
  75.     }
  76.     else
  77.     {
  78.         tail->next = new Node{ data };
  79.         tail = tail->next;
  80.     }
  81.     ++size;
  82. }
  83.  
  84. template <typename ElemT>
  85. void ListFw<ElemT>::push_front(const ElemT& data)
  86. {
  87.     head = new Node{ data, head };
  88.     ++size;
  89.  
  90.     if (!tail)
  91.     {
  92.         tail = head;
  93.     }
  94. }
  95.  
  96. template <typename ElemT>
  97. bool ListFw<ElemT>::pop_front()
  98. {
  99.     if (head)
  100.     {
  101.         Node* tmp{ head };
  102.         head = head->next;
  103.         delete tmp;
  104.         --size;
  105.         return true;
  106.     }
  107.     return false;
  108. }
  109.  
  110. template <typename ElemT>
  111. bool ListFw<ElemT>::pop_back()
  112. {
  113.     if (tail)
  114.     {
  115.         if (tail == head)
  116.         {
  117.             delete tail;
  118.             size = 0;
  119.             tail = nullptr;
  120.             head = nullptr;
  121.             return true;
  122.         }
  123.  
  124.         //Node* curr{ head };
  125.  
  126.         //while (curr)
  127.         for (Node* curr{ head }; curr; curr = curr->next)
  128.         {
  129.             if (curr->next == tail)
  130.             {
  131.                 delete tail;
  132.                 curr->next = nullptr;
  133.                 tail = curr;
  134.                 --size;
  135.                 return true;
  136.             }
  137.             //curr = curr->next;
  138.         }
  139.     }
  140.     return false;
  141. }
  142.  
  143. template <typename ElemT>
  144. void ListFw<ElemT>::clear()
  145. {
  146.     while (tail)
  147.     {
  148.         std::cout << "Clear for: " << back() << '\n';
  149.         pop_back();
  150.     }
  151. }
  152.  
  153. template <typename ElemT>
  154. ListFw<ElemT>::~ListFw()
  155. {
  156.     while (head)
  157.     {
  158.         std::cout << "Destruct for: " << front() << '\n';
  159.         pop_front();
  160.     }
  161. }
  162.  
  163. template<class ElemT>
  164. ListFw<ElemT> ListFw<ElemT>::clone()
  165. {
  166.     ListFw objectT;
  167.     for (Node* curr{ head }; curr != nullptr; curr = curr->next) {
  168.         objectT.push_front(curr->data);
  169.     }
  170.     return objectT;
  171. }
  172.  
  173. template<class ElemT>
  174. ListFw<ElemT> ListFw<ElemT>::operator+(ListFw<ElemT>& object)
  175. {
  176.     ListFw objectTest;
  177.    
  178.     for (Node* curr{ head }; curr != nullptr; curr = curr->next) {
  179.         objectTest.push_back(curr->data);
  180.     }
  181.     size += object.size;
  182.     for (Node* curr{ object.head }; curr != nullptr; curr = curr->next) {
  183.         objectTest.push_back(curr->data);
  184.     }
  185.     size += object.size;
  186.     return objectTest;
  187.  
  188. }
  189.  
  190. template<class ElemT>
  191. ListFw<ElemT> ListFw<ElemT>::operator*(ListFw<ElemT>& object)
  192. {
  193.     ListFw objectNew;
  194.     Node* curr{ head };
  195.     for (int index{ 0 }; index < size and curr!=nullptr; ++index, curr = curr->next) {
  196.         Node* currObject{ object.head };
  197.         for (int indexSecond{ 0 }; indexSecond < object.size and currObject!=nullptr; ++indexSecond, currObject = currObject->next) {
  198.             if (curr->data == currObject->data)
  199.             {
  200.                 objectNew.push_front(curr->data);
  201.                 break;
  202.             }
  203.         }
  204.     }
  205.     return objectNew;
  206. }
  207.  
  208. int main()
  209. {
  210.     /*ListFw<int> l;
  211.     std::cout << "Size: " << l.getSize() << '\n';
  212.  
  213.     l.push_back(42);
  214.     l.push_back(33);
  215.  
  216.     std::cout << "Size: " << l.getSize() << '\n';
  217.     std::cout << "Front: " << l.front() << '\n';
  218.     std::cout << "Back: " << l.back() << '\n';
  219.     l.pop_front();
  220.     l.pop_back();
  221.     std::cout << "Size: " << l.getSize() << '\n';
  222.  
  223.     l.push_back(1);
  224.     l.push_back(2);
  225.     l.push_back(3);
  226.     l.push_back(4);
  227.     std::cout << "Size: " << l.getSize() << '\n';
  228.     std::cout << "Front: " << l.front() << '\n';
  229.     std::cout << "Back: " << l.back() << '\n';
  230.  
  231.     l.clear();
  232.     std::cout << "Size: " << l.getSize() << '\n';
  233.  
  234.     l.push_front(1);
  235.     l.push_front(2);
  236.     l.push_front(3);
  237.     l.push_front(4);
  238.     std::cout << "Size: " << l.getSize() << '\n';
  239.     std::cout << "Front: " << l.front() << '\n';
  240.     std::cout << "Back: " << l.back() << '\n';*/
  241.  
  242.     ListFw<int> test1;
  243.     test1.push_front(1);
  244.     test1.push_back(2);
  245.     ListFw<int> test2;
  246.     test2.push_front(3);
  247.     test2.push_back(4);
  248.     ListFw<int> test3;
  249.     test3 = test2.clone();                              //If you want to check code, uncomment from 235 to 237
  250.     //std::cout << "Size: " << test3.getSize() << '\n';
  251.     //std::cout << "Front: " << test3.front() << '\n';
  252.     //std::cout << "Back: " << test3.back() << '\n';
  253.     ListFw<int> test4;                                  //If you want to check code, uncomment from 235 to 237
  254.     test4 = test1 + test2;
  255.     //std::cout << "Size: " << test4.getSize() << '\n';
  256.     //std::cout << "Front: " << test4.front() << '\n';
  257.     //std::cout << "Back: " << test4.back() << '\n';
  258.     ListFw<int> test5; //Empty be after actions and be error
  259.     test5 = test1 * test2;
  260.     //std::cout << "Size: " << test5.getSize() << '\n';
  261.     //std::cout << "Front: " << test5.front() << '\n';
  262.     //std::cout << "Back: " << test5.back() << '\n';
  263.     ListFw<int> test6{ test2 * test3 };
  264.     //std::cout << "Size: " << test6.getSize() << '\n';
  265.     //std::cout << "Front: " << test6.front() << '\n';
  266.     //std::cout << "Back: " << test6.back() << '\n';
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement