added basic insertion operations

This commit is contained in:
Robin Müller 2020-06-25 17:17:24 +02:00
parent f826ada774
commit d1b9ab5126

View File

@ -58,7 +58,7 @@ public:
this->next = next; this->next = next;
} }
virtual void setEnd() { virtual void setLast() {
this->next = nullptr; this->next = nullptr;
} }
@ -89,10 +89,23 @@ public:
return ElementIterator::Iterator(start); return ElementIterator::Iterator(start);
} }
/** Returns iterator to nulltr */
ElementIterator end() const { ElementIterator end() const {
return ElementIterator::Iterator(); return ElementIterator::Iterator();
} }
/**
* Returns last element in singly linked list.
* @return
*/
ElementIterator back() const {
LinkedElement<T> *element = start;
while (element != nullptr) {
element = element->getNext();
}
return ElementIterator::Iterator(element);
}
size_t getSize() const { size_t getSize() const {
size_t size = 0; size_t size = 0;
LinkedElement<T> *element = start; LinkedElement<T> *element = start;
@ -102,15 +115,37 @@ public:
} }
return size; return size;
} }
void setStart(LinkedElement<T>* setStart) { void setStart(LinkedElement<T>* firstElement) {
start = setStart; start = firstElement;
} }
void setEnd(LinkedElement<T>* setEnd) { void setNext(LinkedElement<T>* currentElement,
setEnd->setEnd(); LinkedElement<T>* nextElement) {
currentElement->setNext(nextElement);
}
void setEnd(LinkedElement<T>* lastElement) {
lastElement->setLast();
} }
// SHOULDDO: Insertion operation ? void insertElement(LinkedElement<T>* element, size_t position) {
LinkedElement<T> *currentElement = start;
for(size_t count = 0; count < position; count++) {
if(currentElement == nullptr) {
return;
}
currentElement = currentElement->getNext();
}
LinkedElement<T>* elementAfterCurrent = currentElement->next;
currentElement->setNext(element);
if(elementAfterCurrent != nullptr) {
element->setNext(elementAfterCurrent);
}
}
void insertBack(LinkedElement<T>* lastElement) {
back().value->setNext(lastElement);
}
protected: protected:
LinkedElement<T> *start = nullptr; LinkedElement<T> *start = nullptr;