Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include "func.hpp"
- template<typename T>
- List<T>::List()
- {
- Size = 0;
- head = nullptr;
- }
- template<typename T>
- List<T>::~List()
- {
- clear();
- }
- template<typename T>
- void List<T>::push_back(T data)
- {
- if(head == nullptr)
- {
- head = tail = new Node(data);
- }
- else
- {
- tail->_pNext = new Node(data);
- tail = tail->_pNext;
- }
- ++Size;
- }
- template<typename T>
- void List<T>::push_front(T data)
- {
- if(head == nullptr)
- {
- head = tail = new Node(data);
- }
- else
- {
- Node* new_node = new Node();
- new_node->_pNext = head;
- new_node->_data = data;
- head = new_node;
- ++Size;
- }
- }
- template<typename T>
- T& List<T>::operator[](const int index)
- {
- Node* current = head;
- int counter = 0;
- while (counter != index)
- {
- current = current->_pNext;
- counter++;
- }
- return current->_data;
- }
- template<typename T>
- void List<T>::pop_front()
- {
- Node* temp = head;
- head = head->_pNext;
- delete temp;
- --Size;
- }
- template<typename T>
- void List<T>::pop_back()
- {
- Node* cur_node = head;
- while(cur_node->_pNext != tail)
- {
- cur_node = cur_node->_pNext;
- }
- delete tail;
- cur_node->_pNext = nullptr;
- tail = cur_node;
- --Size;
- }
- template<typename T>
- void List<T>::insert(T data, int index)
- {
- if(index == 0)
- {
- push_front(data);
- }
- else
- {
- Node* previous = head;
- for (int i = 0; i < index - 1; i++)
- {
- previous = previous->_pNext;
- }
- Node* new_elem = new Node(data);
- new_elem->_pNext = previous->_pNext;
- previous->_pNext = new_elem;
- ++Size;
- }
- }
- template<typename T>
- void List<T>::removeAt(int index)
- {
- if(index == 0)
- {
- pop_front();
- }
- else
- {
- Node* previous = head;
- int count = 0;
- while(count < index - 1)
- {
- previous = previous->_pNext;
- count++;
- }
- Node* toDelete = previous->_pNext;
- previous->_pNext = toDelete->_pNext;
- delete toDelete;
- --Size;
- }
- }
- template<typename T>
- void List<T>::clear()
- {
- while (Size != 0)
- {
- pop_front();
- }
- }
- template<typename T>
- void List<T>::ShiftRight()
- {
- Node* current = head;
- while(current->_pNext != tail)
- {
- current = current->_pNext;
- }
- current->_pNext = nullptr;
- Node* new_curr = head;
- head = tail;
- head->_pNext = new_curr;
- tail = current;
- }
- template<typename T>
- void List<T>::Sort()
- {
- Node* new_curr = head;
- for(int i = 0; i < Size; i++)
- {
- for(int j = 0; j < Size - i - 1; j++)
- {
- if(new_curr->_data > new_curr->_pNext->_data)
- {
- std::swap(new_curr->_data, new_curr->_pNext->_data);
- }
- new_curr = new_curr->_pNext;
- }
- new_curr = head;
- }
- }
- template<typename T>
- void List<T>::Reverse()
- {
- Node* p1 = head;
- Node* p2 = head->_pNext;
- Node* p3 = head->_pNext->_pNext;
- p1->_pNext = nullptr;
- while(p3 != nullptr)
- {
- p2->_pNext = p1;
- p1 = p2;
- p2 = p3;
- p3 = p3->_pNext;
- }
- p2->_pNext = p1;
- tail = head;
- head = p2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement