Advertisement
avr39ripe

cppListFwFindElement

Aug 6th, 2021
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 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.     void print()
  33.     {
  34.         for (auto curr{ head }; curr; curr = curr->next)
  35.         {
  36.             std::cout << curr->data << ' ';
  37.         }
  38.         std::cout << '\n';
  39.     }
  40.  
  41.     //template <typename ElemT>
  42.     //int ListFw<ElemT>::find(const ElemT elem) const {
  43.     //  int indx{ 0 };
  44.     //  for (Node* curr{ head }; curr->data != elem; curr = curr->next, ++indx) { if (!curr->next) { return -1; } }
  45.     //  return indx;
  46.  
  47.     //}
  48.  
  49.     int findElement(const ElemT elem)
  50.     {
  51.         auto curr{ head };
  52.         int idx{ 0 };
  53.         for (; curr; curr = curr->next, ++idx)
  54.         {
  55.             if (curr->data == elem)
  56.             {
  57.                 return idx;
  58.             }
  59.         }
  60.         return -1;
  61.     }
  62.  
  63. };
  64.  
  65. template <typename ElemT>
  66. void ListFw<ElemT>::push_front(const ElemT& data)
  67. {
  68.     head = new Node{ data, head };
  69.  
  70.     if (!tail)
  71.     {
  72.         tail = head;
  73.     }
  74.     ++size;
  75. }
  76.  
  77. template <typename ElemT>
  78. void ListFw<ElemT>::push_back(const ElemT& data)
  79. {
  80.     if (!head)
  81.     {
  82.         head = new Node{ data, head };
  83.         tail = head;
  84.     }
  85.     else
  86.     {
  87.         tail->next = new Node{ data, nullptr };
  88.         tail = tail->next;
  89.     }
  90.     ++size;
  91. }
  92.  
  93. template <typename ElemT>
  94. bool ListFw<ElemT>::pop_front()
  95. {
  96.     if (head)
  97.     {
  98.         Node* oldHead{ head };
  99.         head = head->next;
  100.         delete oldHead;
  101.         --size;
  102.         if (!size) { tail = nullptr; }
  103.         return true;
  104.     }
  105.     return false;
  106. }
  107.  
  108. template <typename ElemT>
  109. bool ListFw<ElemT>::pop_back()
  110. {
  111.     if (tail)
  112.     {
  113.         if (tail == head)
  114.         {
  115.             delete tail;
  116.             head = nullptr;
  117.             tail = nullptr;
  118.             size = 0;
  119.             return true;
  120.         }
  121.  
  122.         Node* curr{ head };
  123.         for (; !(curr->next == tail); curr = curr->next);
  124.        
  125.         delete tail;
  126.         curr->next = nullptr;
  127.         tail = curr;
  128.         --size;
  129.         return true;
  130.     }
  131.     return false;
  132. }
  133.  
  134. template <typename ElemT>
  135. void ListFw<ElemT>::clear()
  136. {
  137.     while (head)
  138.     {
  139.         std::cout << "Clear for: " << front() << '\n';
  140.         pop_front();
  141.     }
  142. }
  143.  
  144. template <typename ElemT>
  145. ListFw<ElemT>::~ListFw()
  146. {
  147.     // BAD! VERY INEFFICIENT!
  148.     while (tail)
  149.     {
  150.         std::cout << "Destruct for: " << back() << '\n';
  151.         pop_back();
  152.     }
  153. }
  154.  
  155.  
  156. template <typename ElemT>
  157. void printListState(const ListFw<ElemT>& l)
  158. {
  159.     std::cout << "Size: " << l.get_size() << '\n';
  160.     std::cout << "Front: " << l.front() << '\n';
  161.     std::cout << "Back: " << l.back() << '\n';
  162. }
  163.  
  164. int getRandom(int start, int end)
  165. {
  166.     return start + rand() % (end - start);
  167. }
  168.  
  169.  
  170. int main()
  171. {
  172.     const int maxElements{ 15 };
  173.     ListFw<int> l;
  174.        
  175.     for (int i{ 0 }; i < maxElements; l.push_back(getRandom(0,10)),++i);
  176.     l.print();
  177.    
  178.     int key;
  179.     std::cout << "Enter number to find in list[0..10]\n";
  180.     std::cin >> key;
  181.  
  182.     int foundIdx{ l.findElement(key) };
  183.  
  184.     if (foundIdx < 0)
  185.     {
  186.         std::cout << "Number not in List!\n";
  187.     }
  188.     else
  189.     {
  190.         std::cout << "Found at idx: " <<  foundIdx << '\n';
  191.     }
  192.    
  193.     return 0;
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement