SHARE
TWEET

List.h

a guest Dec 10th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <iostream>
  3. #include "Book.h"
  4. #include <iomanip>
  5. #include "DisciplineCount.h"
  6.  
  7. using std::endl;
  8. using std::setw;
  9.  
  10. template<typename T>
  11. class List
  12. {
  13. public:
  14.     List();
  15.     void pushBack(const T& data);
  16.     void pushFront(const T& data);
  17.     void popFront();
  18.     void popBack();
  19.     void insert(const T& data, const size_t index);
  20.     void insertAmountSort(const T& dataOut);
  21.     //void insertDisSort(const T& dataout);
  22.     void removeAt(const int index);
  23.     void clear();
  24.     int getSize();
  25.     T& operator[](const int index);
  26.     friend class Book;
  27.     ~List();
  28. private:
  29.  
  30.     template<typename T>
  31.     class Node
  32.     {
  33.     public:
  34.         Node *pNext_;
  35.         T data_;
  36.  
  37.         Node(T data = T(), Node *pNext = nullptr)
  38.         {
  39.             this->data_ = data;
  40.             this->pNext_ = pNext;
  41.         }
  42.  
  43.     };
  44.     size_t size_;
  45.     Node<T> *head_;
  46. };
  47.  
  48. template<typename T>
  49. List<T>::List()
  50. {
  51.     size_ = 0;
  52.     head_ = nullptr;
  53. }
  54.  
  55. template<typename T>
  56. void List<T>::pushBack(const T& data)
  57. {
  58.     if (head_ == nullptr)
  59.     {
  60.         head_ = new Node<T>(data);
  61.     }
  62.     else
  63.     {
  64.         Node<T>* current = this->head_;
  65.         while (current->pNext_ != nullptr)
  66.         {
  67.             current = current->pNext_;
  68.         }
  69.         current->pNext_ = new Node<T>(data);
  70.     }
  71.     size_++;
  72. }
  73.  
  74. template<typename T>
  75. void List<T>::pushFront(const T& data)
  76. {
  77.     head_ = new Node<T>(data, head_);
  78.     size_++;
  79. }
  80.  
  81. template<typename T>
  82. void List<T>::popFront()
  83. {
  84.     Node<T>* temp = head_;
  85.  
  86.     head_ = head_->pNext_;
  87.  
  88.     delete temp;
  89.  
  90.     size_--;
  91. }
  92.  
  93. template<typename T>
  94. void List<T>::popBack()
  95. {
  96.     removeAt(size_ - 1);
  97. }
  98.  
  99. template<typename T>
  100. void List<T>::insert(const T& data, const size_t index)
  101. {
  102.     if (index == 0)
  103.     {
  104.         pushFront(data);
  105.     }
  106.     else
  107.     {
  108.         Node<T>* previous = this->head_;
  109.         for (size_t i = 0; i < index - 1; i++)
  110.         {
  111.             previous = previous->pNext_;
  112.         }
  113.         previous->pNext_ = new Node<T>(data, previous->pNext_);
  114.         size_++;
  115.     }
  116. }
  117.  
  118. template<typename T>
  119. void List<T>::insertAmountSort(const T& dataOut)
  120. {
  121.     if (size_ == 0)
  122.     {
  123.         pushFront(dataOut);
  124.         return;
  125.     }
  126.     else
  127.     {
  128.         Node<T>* current = this->head_;
  129.         for (size_t i = 0; i < size_; i++)
  130.         {
  131.             if (dataOut < current->data_)
  132.             {
  133.                 if (i == 0)
  134.                 {
  135.                     pushFront(dataOut);
  136.                     return;
  137.                 }
  138.                 else
  139.                 {
  140.                     insert(dataOut, i);
  141.                     return;
  142.                 }
  143.             }
  144.             current = current->pNext_;
  145.         }
  146.         pushBack(dataOut);
  147.     }
  148.  
  149. }
  150.  
  151. //template<typename T>
  152. //void List<T>::insertDisSort(const T& dataout)
  153. //{
  154. //  Book temp = dataout;
  155. //}
  156.  
  157.  
  158. template<typename T>
  159. void List<T>::removeAt(const int index)
  160. {
  161.     if (index == 0)
  162.     {
  163.         popFront();
  164.     }
  165.     else
  166.     {
  167.         Node<T>* previous = this->head_;
  168.         for (size_t i = 0; i < index - 1; i++)
  169.         {
  170.             previous = previous->pNext_;
  171.         }
  172.         Node<T>* toDelete = previous->pNext_;
  173.         previous->pNext_ = toDelete->pNext_;
  174.         delete toDelete;
  175.         size_--;
  176.     }
  177. }
  178.  
  179. template<typename T>
  180. void List<T>::clear()
  181. {
  182.     while (size_)
  183.     {
  184.         popFront();
  185.     }
  186. }
  187.  
  188. template<typename T>
  189. int List<T>::getSize()
  190. {
  191.     return size_;
  192. }
  193.  
  194. template<typename T>
  195. T& List<T>::operator[](const int index)
  196. {
  197.     int counter = 0;
  198.     Node<T>* current = this->head_;
  199.     while (current != nullptr)
  200.     {
  201.         if (counter == index)
  202.         {
  203.             return current->data_;
  204.         }
  205.         current = current->pNext_;
  206.         counter++;
  207.     }
  208. }
  209.  
  210. template<typename T>
  211. List<T>::~List()
  212. {
  213.     clear();
  214. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top