#ifndef FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_ #define FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_ #include #include /** * @brief Linked list data structure, * each entry has a pointer to the next entry (singly) * @ingroup container */ template class LinkedElement { public: T *value; class Iterator { public: LinkedElement *value; Iterator() : value(NULL) { } Iterator(LinkedElement *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; } }; LinkedElement(T* setElement, LinkedElement* setNext = nullptr): value(setElement), next(setNext) {} virtual ~LinkedElement(){} virtual LinkedElement* getNext() const { return next; } virtual void setNext(LinkedElement* next) { this->next = next; } virtual void setEnd() { this->next = nullptr; } LinkedElement* begin() { return this; } LinkedElement* end() { return nullptr; } private: LinkedElement *next; }; template class SinglyLinkedList { public: using ElementIterator = typename LinkedElement::Iterator; SinglyLinkedList() {} SinglyLinkedList(ElementIterator start) : start(start.value) {} SinglyLinkedList(LinkedElement* startElement) : start(startElement) {} ElementIterator begin() const { return ElementIterator::Iterator(start); } typename ElementIterator::Iterator end() const { return ElementIterator::Iterator(); } uint32_t getSize() const { uint32_t size = 0; LinkedElement *element = start; while (element != NULL) { size++; element = element->getNext(); } return size; } void setStart(LinkedElement* setStart) { start = setStart; } void setEnd(LinkedElement* setEnd) { setEnd->setEnd(); } protected: LinkedElement *start = nullptr; }; #endif /* SINGLYLINKEDLIST_H_ */