singly linked list update
This commit is contained in:
parent
6d99ab3df3
commit
15052cb333
@ -16,13 +16,8 @@ public:
|
|||||||
class Iterator {
|
class Iterator {
|
||||||
public:
|
public:
|
||||||
LinkedElement<T> *value = nullptr;
|
LinkedElement<T> *value = nullptr;
|
||||||
//! Creates an uninitialized iterator which points to nullptr.
|
|
||||||
Iterator() {}
|
Iterator() {}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize iterator at specified linked element.
|
|
||||||
* @param element
|
|
||||||
*/
|
|
||||||
Iterator(LinkedElement<T> *element) :
|
Iterator(LinkedElement<T> *element) :
|
||||||
value(element) {
|
value(element) {
|
||||||
}
|
}
|
||||||
@ -77,11 +72,6 @@ private:
|
|||||||
LinkedElement *next;
|
LinkedElement *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief SinglyLinkedList data structure which keeps a pointer to its
|
|
||||||
* first element to perform all operations.
|
|
||||||
* @tparam T
|
|
||||||
*/
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class SinglyLinkedList {
|
class SinglyLinkedList {
|
||||||
public:
|
public:
|
||||||
@ -99,7 +89,16 @@ public:
|
|||||||
return ElementIterator::Iterator(start);
|
return ElementIterator::Iterator(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns iterator to nulltr */
|
||||||
ElementIterator end() const {
|
ElementIterator end() const {
|
||||||
|
return ElementIterator::Iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns last element in singly linked list.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
ElementIterator back() const {
|
||||||
LinkedElement<T> *element = start;
|
LinkedElement<T> *element = start;
|
||||||
while (element != nullptr) {
|
while (element != nullptr) {
|
||||||
element = element->getNext();
|
element = element->getNext();
|
||||||
@ -116,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SHOULDDO: Insertion operation ?
|
void setLast(LinkedElement<T>* lastElement) {
|
||||||
|
lastElement->setEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user