Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include "Book.h"
- #include <iomanip>
- #include "DisciplineCount.h"
- using std::endl;
- using std::setw;
- template<typename T>
- class List
- {
- public:
- List();
- void pushBack(const T& data);
- void pushFront(const T& data);
- void popFront();
- void popBack();
- void insert(const T& data, const size_t index);
- void insertAmountSort(const T& dataOut);
- //void insertDisSort(const T& dataout);
- void removeAt(const int index);
- void clear();
- int getSize();
- T& operator[](const int index);
- friend class Book;
- ~List();
- private:
- template<typename T>
- class Node
- {
- public:
- Node *pNext_;
- T data_;
- Node(T data = T(), Node *pNext = nullptr)
- {
- this->data_ = data;
- this->pNext_ = pNext;
- }
- };
- size_t size_;
- Node<T> *head_;
- };
- template<typename T>
- List<T>::List()
- {
- size_ = 0;
- head_ = nullptr;
- }
- template<typename T>
- void List<T>::pushBack(const T& data)
- {
- if (head_ == nullptr)
- {
- head_ = new Node<T>(data);
- }
- else
- {
- Node<T>* current = this->head_;
- while (current->pNext_ != nullptr)
- {
- current = current->pNext_;
- }
- current->pNext_ = new Node<T>(data);
- }
- size_++;
- }
- template<typename T>
- void List<T>::pushFront(const T& data)
- {
- head_ = new Node<T>(data, head_);
- size_++;
- }
- template<typename T>
- void List<T>::popFront()
- {
- Node<T>* temp = head_;
- head_ = head_->pNext_;
- delete temp;
- size_--;
- }
- template<typename T>
- void List<T>::popBack()
- {
- removeAt(size_ - 1);
- }
- template<typename T>
- void List<T>::insert(const T& data, const size_t index)
- {
- if (index == 0)
- {
- pushFront(data);
- }
- else
- {
- Node<T>* previous = this->head_;
- for (size_t i = 0; i < index - 1; i++)
- {
- previous = previous->pNext_;
- }
- previous->pNext_ = new Node<T>(data, previous->pNext_);
- size_++;
- }
- }
- template<typename T>
- void List<T>::insertAmountSort(const T& dataOut)
- {
- if (size_ == 0)
- {
- pushFront(dataOut);
- return;
- }
- else
- {
- Node<T>* current = this->head_;
- for (size_t i = 0; i < size_; i++)
- {
- if (dataOut < current->data_)
- {
- if (i == 0)
- {
- pushFront(dataOut);
- return;
- }
- else
- {
- insert(dataOut, i);
- return;
- }
- }
- current = current->pNext_;
- }
- pushBack(dataOut);
- }
- }
- //template<typename T>
- //void List<T>::insertDisSort(const T& dataout)
- //{
- // Book temp = dataout;
- //}
- template<typename T>
- void List<T>::removeAt(const int index)
- {
- if (index == 0)
- {
- popFront();
- }
- else
- {
- Node<T>* previous = this->head_;
- for (size_t i = 0; i < index - 1; i++)
- {
- previous = previous->pNext_;
- }
- Node<T>* toDelete = previous->pNext_;
- previous->pNext_ = toDelete->pNext_;
- delete toDelete;
- size_--;
- }
- }
- template<typename T>
- void List<T>::clear()
- {
- while (size_)
- {
- popFront();
- }
- }
- template<typename T>
- int List<T>::getSize()
- {
- return size_;
- }
- template<typename T>
- T& List<T>::operator[](const int index)
- {
- int counter = 0;
- Node<T>* current = this->head_;
- while (current != nullptr)
- {
- if (counter == index)
- {
- return current->data_;
- }
- current = current->pNext_;
- counter++;
- }
- }
- template<typename T>
- List<T>::~List()
- {
- clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement