diff options
Autocommit at 02.05.2022 @ 22:00
2 files changed, 28 insertions, 0 deletions
| -rw-r--r-- | List/OrderedList.cpp | 12 | ||||
| -rw-r--r-- | List/OrderedList.h | 16 |
diff --git a/List/OrderedList.cpp b/List/OrderedList.cpp new file mode 100644 index 0000000..f3a7450 --- /dev/null +++ b/List/OrderedList.cpp @@ -0,0 +1,12 @@ +#include "OrderedList.h" + +template <class T> +bool OrderedList<T>::insert(const T& element) { + if (List<T>::length == List<T>::count) List<T>::resize(); + + unsigned insertionInd = 0; + while (List<T>::elements[insertionInd].compare(element) < 0 && insertionInd < List<T>::count) + insertionInd++; + + return List<T>::insertAt(element, insertionInd); +} diff --git a/List/OrderedList.h b/List/OrderedList.h new file mode 100644 index 0000000..22f05df --- /dev/null +++ b/List/OrderedList.h @@ -0,0 +1,16 @@ +#ifndef HEADER_ORDEREDLIST +#define HEADER_ORDEREDLIST + +#include "List.h" + +template <class T> +class OrderedList : public List<T> { +public: + void add(const T& element) = delete; + bool insertAt(const T& element, unsigned index) = delete; + + bool insert(const T& element); + // unsigned findIndex(const T& element) const; TODO: binary search +}; + +#endif |
