From eb4880f60348969b2b70147c97e545577a32c7df Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Wed, 17 Jun 2020 20:53:10 +0200 Subject: [PATCH 1/5] singly linked list improvements --- container/SinglyLinkedList.h | 69 +++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/container/SinglyLinkedList.h b/container/SinglyLinkedList.h index 0a2e0531d..3c0078fc4 100644 --- a/container/SinglyLinkedList.h +++ b/container/SinglyLinkedList.h @@ -1,10 +1,13 @@ -#ifndef SINGLYLINKEDLIST_H_ -#define SINGLYLINKEDLIST_H_ +#ifndef FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_ +#define FRAMEWORK_CONTAINER_SINGLYLINKEDLIST_H_ + +#include +#include -#include -#include /** - * \ingroup container + * @brief Linked list data structure, + * each entry has a pointer to the next entry (singly) + * @ingroup container */ template class LinkedElement { @@ -12,11 +15,8 @@ public: T *value; class Iterator { public: - LinkedElement *value; - Iterator() : - value(NULL) { - - } + LinkedElement *value = nullptr; + Iterator() {} Iterator(LinkedElement *element) : value(element) { @@ -45,12 +45,11 @@ public: } }; - LinkedElement(T* setElement, LinkedElement* setNext = NULL) : value(setElement), - next(setNext) { - } - virtual ~LinkedElement(){ + LinkedElement(T* setElement, LinkedElement* setNext = nullptr): + value(setElement), next(setNext) {} + + virtual ~LinkedElement(){} - } virtual LinkedElement* getNext() const { return next; } @@ -58,11 +57,16 @@ public: virtual void setNext(LinkedElement* next) { this->next = next; } + + virtual void setEnd() { + this->next = nullptr; + } + LinkedElement* begin() { return this; } LinkedElement* end() { - return NULL; + return nullptr; } private: LinkedElement *next; @@ -71,21 +75,21 @@ private: template class SinglyLinkedList { public: - SinglyLinkedList() : - start(NULL) { - } + using ElementIterator = typename LinkedElement::Iterator; + + SinglyLinkedList() {} + + SinglyLinkedList(ElementIterator start) : + start(start.value) {} - SinglyLinkedList(typename LinkedElement::Iterator start) : - start(start.value) { - } SinglyLinkedList(LinkedElement* startElement) : - start(startElement) { - } - typename LinkedElement::Iterator begin() const { - return LinkedElement::Iterator::Iterator(start); + start(startElement) {} + + ElementIterator begin() const { + return ElementIterator::Iterator(start); } - typename LinkedElement::Iterator::Iterator end() const { - return LinkedElement::Iterator::Iterator(); + typename ElementIterator::Iterator end() const { + return ElementIterator::Iterator(); } uint32_t getSize() const { @@ -100,8 +104,15 @@ public: void setStart(LinkedElement* setStart) { start = setStart; } + + void setEnd(LinkedElement* setEnd) { + setEnd->setEnd(); + } + + // SHOULDDO: Insertion operation ? + protected: - LinkedElement *start; + LinkedElement *start = nullptr; }; #endif /* SINGLYLINKEDLIST_H_ */ From f826ada774ed53d31899483b003162c10302527d Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Wed, 24 Jun 2020 11:40:21 +0200 Subject: [PATCH 2/5] some more little changes for single linked list --- container/SinglyLinkedList.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/container/SinglyLinkedList.h b/container/SinglyLinkedList.h index 3c0078fc4..a78a14670 100644 --- a/container/SinglyLinkedList.h +++ b/container/SinglyLinkedList.h @@ -88,14 +88,15 @@ public: ElementIterator begin() const { return ElementIterator::Iterator(start); } - typename ElementIterator::Iterator end() const { + + ElementIterator end() const { return ElementIterator::Iterator(); } - uint32_t getSize() const { - uint32_t size = 0; + size_t getSize() const { + size_t size = 0; LinkedElement *element = start; - while (element != NULL) { + while (element != nullptr) { size++; element = element->getNext(); } From d1b9ab51263e10f5486fe2e8f53afe8670a3087b Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Thu, 25 Jun 2020 17:17:24 +0200 Subject: [PATCH 3/5] added basic insertion operations --- container/SinglyLinkedList.h | 47 +++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/container/SinglyLinkedList.h b/container/SinglyLinkedList.h index a78a14670..618a21e17 100644 --- a/container/SinglyLinkedList.h +++ b/container/SinglyLinkedList.h @@ -58,7 +58,7 @@ public: this->next = next; } - virtual void setEnd() { + virtual void setLast() { this->next = nullptr; } @@ -89,10 +89,23 @@ public: return ElementIterator::Iterator(start); } + /** Returns iterator to nulltr */ ElementIterator end() const { return ElementIterator::Iterator(); } + /** + * Returns last element in singly linked list. + * @return + */ + ElementIterator back() const { + LinkedElement *element = start; + while (element != nullptr) { + element = element->getNext(); + } + return ElementIterator::Iterator(element); + } + size_t getSize() const { size_t size = 0; LinkedElement *element = start; @@ -102,15 +115,37 @@ public: } return size; } - void setStart(LinkedElement* setStart) { - start = setStart; + void setStart(LinkedElement* firstElement) { + start = firstElement; } - void setEnd(LinkedElement* setEnd) { - setEnd->setEnd(); + void setNext(LinkedElement* currentElement, + LinkedElement* nextElement) { + currentElement->setNext(nextElement); + } + + void setEnd(LinkedElement* lastElement) { + lastElement->setLast(); } - // SHOULDDO: Insertion operation ? + void insertElement(LinkedElement* element, size_t position) { + LinkedElement *currentElement = start; + for(size_t count = 0; count < position; count++) { + if(currentElement == nullptr) { + return; + } + currentElement = currentElement->getNext(); + } + LinkedElement* elementAfterCurrent = currentElement->next; + currentElement->setNext(element); + if(elementAfterCurrent != nullptr) { + element->setNext(elementAfterCurrent); + } + } + + void insertBack(LinkedElement* lastElement) { + back().value->setNext(lastElement); + } protected: LinkedElement *start = nullptr; From d7e157d90817d26a1546d78fec56d658073465ad Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Mon, 13 Jul 2020 19:53:44 +0200 Subject: [PATCH 4/5] switch setLast and setEnd --- container/SinglyLinkedList.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/container/SinglyLinkedList.h b/container/SinglyLinkedList.h index 618a21e17..7d5fc4a9d 100644 --- a/container/SinglyLinkedList.h +++ b/container/SinglyLinkedList.h @@ -58,7 +58,7 @@ public: this->next = next; } - virtual void setLast() { + virtual void setEnd() { this->next = nullptr; } @@ -124,8 +124,8 @@ public: currentElement->setNext(nextElement); } - void setEnd(LinkedElement* lastElement) { - lastElement->setLast(); + void setLast(LinkedElement* lastElement) { + lastElement->setEnd(); } void insertElement(LinkedElement* element, size_t position) { From 5df88eb73be77446e520d542e062ebcc4728cd78 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 28 Jul 2020 12:20:23 +0200 Subject: [PATCH 5/5] singlyl inked list bugfix --- container/SinglyLinkedList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/SinglyLinkedList.h b/container/SinglyLinkedList.h index 7d5fc4a9d..eb6ae276a 100644 --- a/container/SinglyLinkedList.h +++ b/container/SinglyLinkedList.h @@ -100,7 +100,7 @@ public: */ ElementIterator back() const { LinkedElement *element = start; - while (element != nullptr) { + while (element->getNext() != nullptr) { element = element->getNext(); } return ElementIterator::Iterator(element);