Advertisement
avr39ripe

cppListFwClass

Aug 5th, 2021
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename ElemT>
  4. class ListFw
  5. {
  6.     class Node
  7.     {
  8.         ElemT data;
  9.         Node* next;
  10.     public:
  11.         Node(const ElemT& dataP, Node* nextP = nullptr) : data{ dataP }, next{ nextP }{}
  12.         friend ListFw;
  13.     };
  14.  
  15.     Node* head;
  16.     Node* tail;
  17.     size_t size;
  18. public:
  19.     ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
  20.     bool empty()const { return size == 0; }
  21.     size_t get_size()const { return size; }
  22.     ElemT& front() { return head->data; }
  23.     const ElemT& front()const { return head->data; }
  24.     ElemT& back() { return tail->data; }
  25.     const ElemT& back()const { return tail->data; }
  26.     void push_front(const ElemT& data);
  27.     void push_back(const ElemT& data);
  28.     bool pop_front();
  29.     bool pop_back();
  30.     void clear();
  31.     ~ListFw();
  32. };
  33.  
  34. template <typename ElemT>
  35. void ListFw<ElemT>::push_front(const ElemT& data)
  36. {
  37.     head = new Node{ data, head };
  38.  
  39.     if (!tail)
  40.     {
  41.         tail = head;
  42.     }
  43.     ++size;
  44. }
  45.  
  46. template <typename ElemT>
  47. void ListFw<ElemT>::push_back(const ElemT& data)
  48. {
  49.     if (!head)
  50.     {
  51.         head = new Node{ data, head };
  52.         tail = head;
  53.     }
  54.     else
  55.     {
  56.         tail->next = new Node{ data, nullptr };
  57.         tail = tail->next;
  58.     }
  59.     ++size;
  60. }
  61.  
  62. template <typename ElemT>
  63. bool ListFw<ElemT>::pop_front()
  64. {
  65.     if (head)
  66.     {
  67.         Node* oldHead{ head };
  68.         head = head->next;
  69.         delete oldHead;
  70.         --size;
  71.         if (!size) { tail = nullptr; }
  72.         return true;
  73.     }
  74.     return false;
  75. }
  76.  
  77. template <typename ElemT>
  78. bool ListFw<ElemT>::pop_back()
  79. {
  80.     if (tail)
  81.     {
  82.         if (tail == head)
  83.         {
  84.             delete tail;
  85.             head = nullptr;
  86.             tail = nullptr;
  87.             size = 0;
  88.             return true;
  89.         }
  90.  
  91.         Node* curr{ head };
  92.         for (; !(curr->next == tail); curr = curr->next);
  93.        
  94.         delete tail;
  95.         curr->next = nullptr;
  96.         tail = curr;
  97.         --size;
  98.         return true;
  99.     }
  100.     return false;
  101. }
  102.  
  103. template <typename ElemT>
  104. void ListFw<ElemT>::clear()
  105. {
  106.     while (head)
  107.     {
  108.         std::cout << "Clear for: " << front() << '\n';
  109.         pop_front();
  110.     }
  111. }
  112.  
  113. template <typename ElemT>
  114. ListFw<ElemT>::~ListFw()
  115. {
  116.     // BAD! VERY INEFFICIENT!
  117.     while (tail)
  118.     {
  119.         std::cout << "Destruct for: " << back() << '\n';
  120.         pop_back();
  121.     }
  122. }
  123.  
  124. template <typename ElemT>
  125. void printListState(const ListFw<ElemT>& l)
  126. {
  127.     std::cout << "Size: " << l.get_size() << '\n';
  128.     std::cout << "Front: " << l.front() << '\n';
  129.     std::cout << "Back: " << l.back() << '\n';
  130. }
  131.  
  132. int main()
  133. {
  134.     ListFw<int> l;
  135.  
  136.     std::cout << "Size: " << l.get_size() << '\n';
  137.    
  138.     // [42,33]
  139.  
  140.     l.push_back(42);
  141.     l.push_back(33);
  142.  
  143.     printListState(l);
  144.  
  145.     l.pop_front();
  146.     l.pop_back();
  147.     std::cout << "Size: " << l.get_size() << '\n';
  148.  
  149.     for (int i{ 0 }; i < 4; l.push_back(i++));
  150.  
  151.     printListState(l);
  152.     l.clear();
  153.  
  154.     // [3,2,1,0]
  155.     for (int i{ 0 }; i < 4; l.push_front(i++));
  156.     printListState(l);
  157.  
  158.     return 0;
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement