Advertisement
Qanar

LL Class_LinkedList.cpp

Apr 11th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include "LinkedList.h"
  2.  
  3. template <class T>
  4. LinkedList<T>::LinkedList() {
  5.     this->init();
  6. }
  7.  
  8. template <class T>
  9. LinkedList<T>::LinkedList(T firstData){
  10.     this->init();
  11. }
  12.  
  13.  
  14. template <class T>
  15. LinkedList<T>::LinkedList(const T  datas[], int size) {
  16.     this->init();
  17.    
  18.     for (int i = 0; i < size; i++) {
  19.         T meinRTL = datas[i];
  20.         this->append(meinRTL);
  21.     }
  22.    
  23. }
  24.  
  25. template <class T>
  26. LinkedList<T>::~LinkedList(){
  27.    
  28. }
  29.  
  30.  
  31. ////////////// DATA STRUCT ///////////////
  32.  
  33. template <class T>
  34. void LinkedList<T>::prepend(T data) {
  35.     genericNode<T>* newNode = new genericNode<T>{data, NULL, NULL};
  36.     newNode->data = data;
  37.    
  38.     this->count++;
  39.     if (this->head != NULL) {
  40.         newNode->next = this->head;
  41.         this->head->prev = newNode;
  42.     } else {
  43.         this->end = newNode;
  44.     }
  45.    
  46.     this->head = newNode;
  47.     this->reset();
  48. }
  49.  
  50. template <class T>
  51. void LinkedList<T>::append(T data) {
  52.     genericNode<T>* newNode = new genericNode<T>{data, NULL, NULL};
  53.     newNode->data = data;
  54.     this->count++;
  55.    
  56.     if (this->end != NULL) {
  57.         newNode->prev = this->end;
  58.         this->end->next = newNode;
  59.     } else {
  60.         this->head = newNode;
  61.     }
  62.    
  63.     this->end = newNode;
  64. }
  65.  
  66.  
  67. ////////////// UTIL ///////////////
  68. template <class T>
  69. void LinkedList<T>::init(){
  70.     this->head = NULL;
  71.     this->current = NULL;
  72.     this->end = NULL;
  73.    
  74.     this->count = 0;
  75.     this->currentIndex = 0;
  76. }
  77.  
  78. template <class T>
  79. void LinkedList<T>::reset(){
  80.     this->current = this->head;
  81.     this->currentIndex = 0;
  82. }
  83.  
  84.  
  85. template <class T>
  86. void LinkedList<T>::printList(){
  87.     this->reset();
  88.    
  89.     while(this->current != NULL) {
  90.         std::cout << this->currentIndex << "\t" ;
  91.         std::cout << this->current->data << std::endl;
  92.         this->current = this->current->next;
  93.         this->currentIndex++;
  94.     }
  95. }
  96.  
  97. template <class T>
  98. void LinkedList<T>::printListAsChar(){
  99.     this->reset();
  100.    
  101.     while(this->current != NULL) {
  102.         std::cout << this->currentIndex << "\t" ;
  103.        
  104.         std::cout << (char) this->current->data << std::endl;
  105.        
  106.         this->current = this->current->next;
  107.         this->currentIndex++;
  108.     }
  109. }
  110.  
  111.  
  112. template <class T>
  113. void LinkedList<T>::printReversedList(){
  114.     this->currentIndex = this->count - 1;
  115.     this->current = this->end;
  116.    
  117.     while(this->current != NULL) {
  118.         std::cout << this->currentIndex << "\t" ;
  119.         std::cout << this->current->data << std::endl;
  120.         this->current = this->current->prev;
  121.         this->currentIndex--;
  122.     }
  123. }
  124.  
  125.  
  126. template <class T>
  127. std::optional<T> LinkedList<T>::getFirst(){
  128.     if (this->head == NULL)
  129.         return std::nullopt;
  130.    
  131.     this->current = this->head;
  132.     this->currentIndex = 0;
  133.     return { this->head->data };
  134. }
  135.  
  136. template <class T>
  137. std::optional<T> LinkedList<T>::getLast(){
  138.     if (this->end == NULL)
  139.         return std::nullopt;
  140.        
  141.     this->current = this->end;
  142.     this->currentIndex = this->count - 1;
  143.     return { this->end->data };
  144. }
  145.  
  146. template <class T>
  147. unsigned int LinkedList<T>::getSize() {
  148.     return this->count;
  149. }
  150.    
  151. template <class T>
  152. std::optional<T> LinkedList<T>::getAt(int index){
  153.    
  154.     if (index < 0)
  155.         return std::nullopt;
  156.     if (index > this->count-1)
  157.         return std::nullopt;
  158.    
  159.     int diffEnd = abs(this->count - 1 - index);
  160.    
  161.     genericNode<T>* startPosition = this->head;
  162.     int startIndex = 0;
  163.     bool shouldSearchReversed = false;
  164.    
  165.     if (this->currentIndex < index ) {
  166.         startPosition = this->current;
  167.         startIndex  = this->currentIndex;
  168.     }
  169.    
  170.     if (diffEnd < index) {
  171.         startPosition = this->end;
  172.         startIndex  = this->count - 1;
  173.         shouldSearchReversed = true;
  174.     }
  175.    
  176.    
  177.     this->current = startPosition;
  178.     this->currentIndex = startIndex;
  179.     if (shouldSearchReversed) {
  180.         while(this->currentIndex > index) {
  181.             this->getPrev();
  182.         }
  183.     } else {
  184.         while(this->currentIndex < index) {
  185.             this->getNext();
  186.         }
  187.     }
  188.    
  189.     return { this->current->data };
  190. }
  191.  
  192. template <class T>
  193. std::optional<T> LinkedList<T>::getNext(){
  194.     this->currentIndex++;
  195.     this->current = this->current->next;
  196. }
  197.  
  198. template <class T>
  199. std::optional<T> LinkedList<T>::getPrev(){
  200.     this->currentIndex--;
  201.     this->current = this->current->prev;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement