2020-06-17 20:53:10 +02:00
|
|
|
#ifndef FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_
|
|
|
|
#define FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
/**
|
2020-06-17 20:53:10 +02:00
|
|
|
* @brief Linked list data structure,
|
|
|
|
* each entry has a pointer to the next entry (singly)
|
|
|
|
* @ingroup container
|
2019-08-28 14:50:24 +02:00
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename T>
|
|
|
|
class LinkedElement {
|
|
|
|
public:
|
|
|
|
T *value;
|
|
|
|
class Iterator {
|
|
|
|
public:
|
2020-06-17 20:53:10 +02:00
|
|
|
LinkedElement<T> *value = nullptr;
|
|
|
|
Iterator() {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
Iterator(LinkedElement<T> *element) :
|
|
|
|
value(element) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator& operator++() {
|
|
|
|
value = value->getNext();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator operator++(int) {
|
|
|
|
Iterator tmp(*this);
|
|
|
|
operator++();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(Iterator other) {
|
|
|
|
return value == other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(Iterator other) {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
T *operator->() {
|
|
|
|
return value->value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-17 20:53:10 +02:00
|
|
|
LinkedElement(T* setElement, LinkedElement<T>* setNext = nullptr):
|
|
|
|
value(setElement), next(setNext) {}
|
|
|
|
|
|
|
|
virtual ~LinkedElement(){}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
virtual LinkedElement* getNext() const {
|
2016-06-15 23:48:41 +02:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setNext(LinkedElement* next) {
|
|
|
|
this->next = next;
|
|
|
|
}
|
2020-06-17 20:53:10 +02:00
|
|
|
|
2020-07-13 19:53:44 +02:00
|
|
|
virtual void setEnd() {
|
2020-06-17 20:53:10 +02:00
|
|
|
this->next = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
LinkedElement* begin() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
LinkedElement* end() {
|
2020-06-17 20:53:10 +02:00
|
|
|
return nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
LinkedElement *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class SinglyLinkedList {
|
|
|
|
public:
|
2020-06-17 20:53:10 +02:00
|
|
|
using ElementIterator = typename LinkedElement<T>::Iterator;
|
|
|
|
|
|
|
|
SinglyLinkedList() {}
|
|
|
|
|
|
|
|
SinglyLinkedList(ElementIterator start) :
|
|
|
|
start(start.value) {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
SinglyLinkedList(LinkedElement<T>* startElement) :
|
2020-06-17 20:53:10 +02:00
|
|
|
start(startElement) {}
|
|
|
|
|
|
|
|
ElementIterator begin() const {
|
|
|
|
return ElementIterator::Iterator(start);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-06-24 11:40:21 +02:00
|
|
|
|
2020-06-25 17:17:24 +02:00
|
|
|
/** Returns iterator to nulltr */
|
2020-06-24 11:40:21 +02:00
|
|
|
ElementIterator end() const {
|
2020-06-17 20:53:10 +02:00
|
|
|
return ElementIterator::Iterator();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 17:17:24 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2020-06-24 11:40:21 +02:00
|
|
|
size_t getSize() const {
|
|
|
|
size_t size = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
LinkedElement<T> *element = start;
|
2020-06-24 11:40:21 +02:00
|
|
|
while (element != nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
size++;
|
|
|
|
element = element->getNext();
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2020-06-25 17:17:24 +02:00
|
|
|
void setStart(LinkedElement<T>* firstElement) {
|
|
|
|
start = firstElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setNext(LinkedElement<T>* currentElement,
|
|
|
|
LinkedElement<T>* nextElement) {
|
|
|
|
currentElement->setNext(nextElement);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-06-17 20:53:10 +02:00
|
|
|
|
2020-07-13 19:53:44 +02:00
|
|
|
void setLast(LinkedElement<T>* lastElement) {
|
|
|
|
lastElement->setEnd();
|
2020-06-17 20:53:10 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 17:17:24 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-06-17 20:53:10 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
protected:
|
2020-06-17 20:53:10 +02:00
|
|
|
LinkedElement<T> *start = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SINGLYLINKEDLIST_H_ */
|