Advertisement
avr39ripe

PV913ListFw

Jul 13th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.27 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();
  32. };
  33.  
  34. template <typename ElemT>
  35. void ListFw<ElemT>::push_back(const ElemT& data)
  36. {
  37.     if (!head)
  38.     {
  39.         head = new Node{ data };
  40.         tail = head;
  41.     }
  42.     else
  43.     {
  44.         tail->next = new Node{ data };
  45.         tail = tail->next;
  46.     }
  47.     ++size;
  48. }
  49.  
  50. template <typename ElemT>
  51. void ListFw<ElemT>::push_front(const ElemT& data)
  52. {
  53.     head = new Node{ data, head };
  54.     ++size;
  55.  
  56.     if (!tail)
  57.     {
  58.         tail = head;
  59.     }
  60. }
  61.  
  62. template <typename ElemT>
  63. bool ListFw<ElemT>::pop_front()
  64. {
  65.     if (head)
  66.     {
  67.         Node* tmp{ head };
  68.         head = head->next;
  69.         delete tmp;
  70.         --size;
  71.         return true;
  72.     }
  73.     return false;
  74. }
  75.  
  76. template <typename ElemT>
  77. bool ListFw<ElemT>::pop_back()
  78. {
  79.     if (tail)
  80.     {
  81.         if (tail == head)
  82.         {
  83.             delete tail;
  84.             size = 0;
  85.             tail = nullptr;
  86.             head = nullptr;
  87.             return true;
  88.         }
  89.  
  90.         //Node* curr{ head };
  91.  
  92.         //while (curr)
  93.         for(Node* curr{ head }; curr; curr = curr->next)
  94.         {
  95.             if (curr->next == tail)
  96.             {
  97.                 delete tail;
  98.                 curr->next = nullptr;
  99.                 tail = curr;
  100.                 --size;
  101.                 return true;
  102.             }
  103.             //curr = curr->next;
  104.         }
  105.     }
  106.     return false;
  107. }
  108.  
  109. template <typename ElemT>
  110. void ListFw<ElemT>::clear()
  111. {
  112.     while (tail)
  113.     {
  114.         std::cout << "Clear for: " << back() << '\n';
  115.         pop_back();
  116.     }
  117. }
  118.  
  119. template <typename ElemT>
  120. ListFw<ElemT>::~ListFw()
  121. {
  122.     while (head)
  123.     {
  124.         std::cout << "Destruct for: " << front() << '\n';
  125.         pop_front();
  126.     }
  127. }
  128.  
  129. int main()
  130. {
  131.     ListFw<int> l;
  132.     std::cout << "Size: " << l.getSize() << '\n';
  133.  
  134.     l.push_back(42);
  135.     l.push_back(33);
  136.  
  137.     std::cout << "Size: " << l.getSize() << '\n';
  138.     std::cout << "Front: " << l.front() << '\n';
  139.     std::cout << "Back: " << l.back() << '\n';
  140.     l.pop_front();
  141.     l.pop_back();
  142.     std::cout << "Size: " << l.getSize() << '\n';
  143.  
  144.     l.push_back(1);
  145.     l.push_back(2);
  146.     l.push_back(3);
  147.     l.push_back(4);
  148.     std::cout << "Size: " << l.getSize() << '\n';
  149.     std::cout << "Front: " << l.front() << '\n';
  150.     std::cout << "Back: " << l.back() << '\n';
  151.  
  152.     l.clear();
  153.     std::cout << "Size: " << l.getSize() << '\n';
  154.  
  155.     l.push_front(1);
  156.     l.push_front(2);
  157.     l.push_front(3);
  158.     l.push_front(4);
  159.     std::cout << "Size: " << l.getSize() << '\n';
  160.     std::cout << "Front: " << l.front() << '\n';
  161.     std::cout << "Back: " << l.back() << '\n';
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement