Merge pull request 'singly linked list improvements' (#108) from KSat/fsfw:mueller_SinglyLinkedListImprovements into master
This commit is contained in:
commit
a1d1d42e88
@ -1,10 +1,13 @@
|
|||||||
#ifndef SINGLYLINKEDLIST_H_
|
#ifndef FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_
|
||||||
#define SINGLYLINKEDLIST_H_
|
#define FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
/**
|
/**
|
||||||
* \ingroup container
|
* @brief Linked list data structure,
|
||||||
|
* each entry has a pointer to the next entry (singly)
|
||||||
|
* @ingroup container
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class LinkedElement {
|
class LinkedElement {
|
||||||
@ -12,11 +15,8 @@ public:
|
|||||||
T *value;
|
T *value;
|
||||||
class Iterator {
|
class Iterator {
|
||||||
public:
|
public:
|
||||||
LinkedElement<T> *value;
|
LinkedElement<T> *value = nullptr;
|
||||||
Iterator() :
|
Iterator() {}
|
||||||
value(NULL) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator(LinkedElement<T> *element) :
|
Iterator(LinkedElement<T> *element) :
|
||||||
value(element) {
|
value(element) {
|
||||||
@ -45,12 +45,11 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
LinkedElement(T* setElement, LinkedElement<T>* setNext = NULL) : value(setElement),
|
LinkedElement(T* setElement, LinkedElement<T>* setNext = nullptr):
|
||||||
next(setNext) {
|
value(setElement), next(setNext) {}
|
||||||
}
|
|
||||||
virtual ~LinkedElement(){
|
virtual ~LinkedElement(){}
|
||||||
|
|
||||||
}
|
|
||||||
virtual LinkedElement* getNext() const {
|
virtual LinkedElement* getNext() const {
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
@ -58,11 +57,16 @@ public:
|
|||||||
virtual void setNext(LinkedElement* next) {
|
virtual void setNext(LinkedElement* next) {
|
||||||
this->next = next;
|
this->next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void setEnd() {
|
||||||
|
this->next = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
LinkedElement* begin() {
|
LinkedElement* begin() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
LinkedElement* end() {
|
LinkedElement* end() {
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
LinkedElement *next;
|
LinkedElement *next;
|
||||||
@ -71,37 +75,80 @@ private:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class SinglyLinkedList {
|
class SinglyLinkedList {
|
||||||
public:
|
public:
|
||||||
SinglyLinkedList() :
|
using ElementIterator = typename LinkedElement<T>::Iterator;
|
||||||
start(NULL) {
|
|
||||||
}
|
SinglyLinkedList() {}
|
||||||
|
|
||||||
|
SinglyLinkedList(ElementIterator start) :
|
||||||
|
start(start.value) {}
|
||||||
|
|
||||||
SinglyLinkedList(typename LinkedElement<T>::Iterator start) :
|
|
||||||
start(start.value) {
|
|
||||||
}
|
|
||||||
SinglyLinkedList(LinkedElement<T>* startElement) :
|
SinglyLinkedList(LinkedElement<T>* startElement) :
|
||||||
start(startElement) {
|
start(startElement) {}
|
||||||
}
|
|
||||||
typename LinkedElement<T>::Iterator begin() const {
|
ElementIterator begin() const {
|
||||||
return LinkedElement<T>::Iterator::Iterator(start);
|
return ElementIterator::Iterator(start);
|
||||||
}
|
|
||||||
typename LinkedElement<T>::Iterator::Iterator end() const {
|
|
||||||
return LinkedElement<T>::Iterator::Iterator();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getSize() const {
|
/** Returns iterator to nulltr */
|
||||||
uint32_t size = 0;
|
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 != NULL) {
|
while (element->getNext() != nullptr) {
|
||||||
|
element = element->getNext();
|
||||||
|
}
|
||||||
|
return ElementIterator::Iterator(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getSize() const {
|
||||||
|
size_t size = 0;
|
||||||
|
LinkedElement<T> *element = start;
|
||||||
|
while (element != nullptr) {
|
||||||
size++;
|
size++;
|
||||||
element = element->getNext();
|
element = element->getNext();
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
void setStart(LinkedElement<T>* setStart) {
|
void setStart(LinkedElement<T>* firstElement) {
|
||||||
start = setStart;
|
start = firstElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setNext(LinkedElement<T>* currentElement,
|
||||||
|
LinkedElement<T>* nextElement) {
|
||||||
|
currentElement->setNext(nextElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
LinkedElement<T> *start = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SINGLYLINKEDLIST_H_ */
|
#endif /* SINGLYLINKEDLIST_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user