#ifndef NALOGA0802_LINKEDLIST_H #define NALOGA0802_LINKEDLIST_H #include "Node.h" template class LinkedList { private: Node *head = nullptr; public: bool isEmpty() const; unsigned int getSize() const; void add(T value); T at(unsigned int index) const; ~LinkedList(); void insertAt(unsigned int index, T value); }; template bool LinkedList::isEmpty() const { return (head == nullptr); } template unsigned int LinkedList::getSize() const { Node *tmp = head; unsigned int ret = 0; while (tmp != nullptr) { ret++; tmp = tmp->getNext(); } return ret; } template void LinkedList::add(T value) { Node *va = new Node(value, nullptr); if (head == nullptr) { head = va; } else { Node *tmp = head; Node *last; while (tmp != nullptr) { last = tmp; tmp = tmp->getNext(); } last->setNext(va); } } template T LinkedList::at(unsigned int index) const { Node *tmp = head; unsigned int count = 0; for (int i = 0; i < index && tmp != nullptr; i++) { tmp = tmp->getNext(); } return tmp->getValue(); } template LinkedList::~LinkedList() { Node *tmp = head; Node *last; while (tmp != nullptr) { last = tmp; tmp = tmp->getNext(); delete last; } delete tmp; } template void LinkedList::insertAt(unsigned int index, T value) { Node *pre; Node *cur = head; Node *temp = new Node(value, nullptr); if(head != nullptr) { for (int i = 0; i < index && cur != nullptr; i++) { pre = cur; cur = cur->getNext(); } pre->setNext(temp); temp->setNext(cur); }else{ head = temp; } } #endif //NALOGA0802_LINKEDLIST_H