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
|
|
|
|
|
|
|
virtual void setEnd() {
|
|
|
|
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-17 20:53:10 +02:00
|
|
|
typename ElementIterator::Iterator end() const {
|
|
|
|
return ElementIterator::Iterator();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getSize() const {
|
|
|
|
uint32_t size = 0;
|
|
|
|
LinkedElement<T> *element = start;
|
|
|
|
while (element != NULL) {
|
|
|
|
size++;
|
|
|
|
element = element->getNext();
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
void setStart(LinkedElement<T>* setStart) {
|
|
|
|
start = setStart;
|
|
|
|
}
|
2020-06-17 20:53:10 +02:00
|
|
|
|
|
|
|
void setEnd(LinkedElement<T>* setEnd) {
|
|
|
|
setEnd->setEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
// SHOULDDO: Insertion operation ?
|
|
|
|
|
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_ */
|