Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "LinkedList.h"
- template <class T>
- LinkedList<T>::LinkedList() {
- this->init();
- }
- template <class T>
- LinkedList<T>::LinkedList(T firstData){
- this->init();
- }
- template <class T>
- LinkedList<T>::LinkedList(const T datas[], int size) {
- this->init();
- for (int i = 0; i < size; i++) {
- T meinRTL = datas[i];
- this->append(meinRTL);
- }
- }
- template <class T>
- LinkedList<T>::~LinkedList(){
- }
- ////////////// DATA STRUCT ///////////////
- template <class T>
- void LinkedList<T>::prepend(T data) {
- genericNode<T>* newNode = new genericNode<T>{data, NULL, NULL};
- newNode->data = data;
- this->count++;
- if (this->head != NULL) {
- newNode->next = this->head;
- this->head->prev = newNode;
- } else {
- this->end = newNode;
- }
- this->head = newNode;
- this->reset();
- }
- template <class T>
- void LinkedList<T>::append(T data) {
- genericNode<T>* newNode = new genericNode<T>{data, NULL, NULL};
- newNode->data = data;
- this->count++;
- if (this->end != NULL) {
- newNode->prev = this->end;
- this->end->next = newNode;
- } else {
- this->head = newNode;
- }
- this->end = newNode;
- }
- ////////////// UTIL ///////////////
- template <class T>
- void LinkedList<T>::init(){
- this->head = NULL;
- this->current = NULL;
- this->end = NULL;
- this->count = 0;
- this->currentIndex = 0;
- }
- template <class T>
- void LinkedList<T>::reset(){
- this->current = this->head;
- this->currentIndex = 0;
- }
- template <class T>
- void LinkedList<T>::printList(){
- this->reset();
- while(this->current != NULL) {
- std::cout << this->currentIndex << "\t" ;
- std::cout << this->current->data << std::endl;
- this->current = this->current->next;
- this->currentIndex++;
- }
- }
- template <class T>
- void LinkedList<T>::printListAsChar(){
- this->reset();
- while(this->current != NULL) {
- std::cout << this->currentIndex << "\t" ;
- std::cout << (char) this->current->data << std::endl;
- this->current = this->current->next;
- this->currentIndex++;
- }
- }
- template <class T>
- void LinkedList<T>::printReversedList(){
- this->currentIndex = this->count - 1;
- this->current = this->end;
- while(this->current != NULL) {
- std::cout << this->currentIndex << "\t" ;
- std::cout << this->current->data << std::endl;
- this->current = this->current->prev;
- this->currentIndex--;
- }
- }
- template <class T>
- std::optional<T> LinkedList<T>::getFirst(){
- if (this->head == NULL)
- return std::nullopt;
- this->current = this->head;
- this->currentIndex = 0;
- return { this->head->data };
- }
- template <class T>
- std::optional<T> LinkedList<T>::getLast(){
- if (this->end == NULL)
- return std::nullopt;
- this->current = this->end;
- this->currentIndex = this->count - 1;
- return { this->end->data };
- }
- template <class T>
- unsigned int LinkedList<T>::getSize() {
- return this->count;
- }
- template <class T>
- std::optional<T> LinkedList<T>::getAt(int index){
- if (index < 0)
- return std::nullopt;
- if (index > this->count-1)
- return std::nullopt;
- int diffEnd = abs(this->count - 1 - index);
- genericNode<T>* startPosition = this->head;
- int startIndex = 0;
- bool shouldSearchReversed = false;
- if (this->currentIndex < index ) {
- startPosition = this->current;
- startIndex = this->currentIndex;
- }
- if (diffEnd < index) {
- startPosition = this->end;
- startIndex = this->count - 1;
- shouldSearchReversed = true;
- }
- this->current = startPosition;
- this->currentIndex = startIndex;
- if (shouldSearchReversed) {
- while(this->currentIndex > index) {
- this->getPrev();
- }
- } else {
- while(this->currentIndex < index) {
- this->getNext();
- }
- }
- return { this->current->data };
- }
- template <class T>
- std::optional<T> LinkedList<T>::getNext(){
- this->currentIndex++;
- this->current = this->current->next;
- }
- template <class T>
- std::optional<T> LinkedList<T>::getPrev(){
- this->currentIndex--;
- this->current = this->current->prev;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement