fsfw/container/SinglyLinkedList.h

155 lines
3.2 KiB
C
Raw Normal View History

2020-06-17 20:53:10 +02:00
#ifndef FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_
#define FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_
#include <cstddef>
#include <cstdint>
/**
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
*/
template<typename T>
class LinkedElement {
public:
T *value;
class Iterator {
public:
2020-06-17 20:53:10 +02:00
LinkedElement<T> *value = nullptr;
Iterator() {}
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(){}
virtual LinkedElement* getNext() const {
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;
}
LinkedElement* begin() {
return this;
}
LinkedElement* end() {
2020-06-17 20:53:10 +02:00
return nullptr;
}
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) {}
SinglyLinkedList(LinkedElement<T>* startElement) :
2020-06-17 20:53:10 +02:00
start(startElement) {}
ElementIterator begin() const {
return ElementIterator::Iterator(start);
}
2020-06-25 17:17:24 +02:00
/** Returns iterator to nulltr */
ElementIterator end() const {
2020-06-17 20:53:10 +02:00
return ElementIterator::Iterator();
}
2020-06-25 17:17:24 +02:00
/**
* Returns last element in singly linked list.
* @return
*/
ElementIterator back() const {
LinkedElement<T> *element = start;
2020-07-28 12:20:23 +02:00
while (element->getNext() != nullptr) {
2020-06-25 17:17:24 +02:00
element = element->getNext();
}
return ElementIterator::Iterator(element);
}
size_t getSize() const {
size_t size = 0;
LinkedElement<T> *element = start;
while (element != nullptr) {
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);
}
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
protected:
2020-06-17 20:53:10 +02:00
LinkedElement<T> *start = nullptr;
};
#endif /* SINGLYLINKEDLIST_H_ */