diff --git a/container/SinglyLinkedList.h b/container/SinglyLinkedList.h index 85a92e6a..7d5fc4a9 100644 --- a/container/SinglyLinkedList.h +++ b/container/SinglyLinkedList.h @@ -16,13 +16,8 @@ public: class Iterator { public: LinkedElement *value = nullptr; - //! Creates an uninitialized iterator which points to nullptr. Iterator() {} - /** - * Initialize iterator at specified linked element. - * @param element - */ Iterator(LinkedElement *element) : value(element) { } @@ -77,11 +72,6 @@ private: LinkedElement *next; }; -/** - * @brief SinglyLinkedList data structure which keeps a pointer to its - * first element to perform all operations. - * @tparam T - */ template class SinglyLinkedList { public: @@ -99,12 +89,21 @@ public: return ElementIterator::Iterator(start); } + /** Returns iterator to nulltr */ ElementIterator end() const { - LinkedElement *element = start; - while (element != nullptr) { - element = element->getNext(); - } - return ElementIterator::Iterator(element); + return ElementIterator::Iterator(); + } + + /** + * Returns last element in singly linked list. + * @return + */ + ElementIterator back() const { + LinkedElement *element = start; + while (element != nullptr) { + element = element->getNext(); + } + return ElementIterator::Iterator(element); } size_t getSize() const { @@ -116,15 +115,37 @@ public: } return size; } - void setStart(LinkedElement* setStart) { - start = setStart; + void setStart(LinkedElement* firstElement) { + start = firstElement; } - void setEnd(LinkedElement* setEnd) { - setEnd->setEnd(); + void setNext(LinkedElement* currentElement, + LinkedElement* nextElement) { + currentElement->setNext(nextElement); + } + + void setLast(LinkedElement* lastElement) { + lastElement->setEnd(); } - // SHOULDDO: Insertion operation ? + void insertElement(LinkedElement* element, size_t position) { + LinkedElement *currentElement = start; + for(size_t count = 0; count < position; count++) { + if(currentElement == nullptr) { + return; + } + currentElement = currentElement->getNext(); + } + LinkedElement* elementAfterCurrent = currentElement->next; + currentElement->setNext(element); + if(elementAfterCurrent != nullptr) { + element->setNext(elementAfterCurrent); + } + } + + void insertBack(LinkedElement* lastElement) { + back().value->setNext(lastElement); + } protected: LinkedElement *start = nullptr;