1

update list interface

This commit is contained in:
2022-07-17 21:02:03 +02:00
parent d87fa5e40a
commit 5dd23fc888

View File

@ -10,23 +10,26 @@
// For things like LinkedList, the operator++ has to be overriden to implement the traversal. // For things like LinkedList, the operator++ has to be overriden to implement the traversal.
template<typename T> template<typename T>
class ListIterator { class ListIterator {
public:
using Type = T;
private: private:
T* ptr; Type* ptr;
public: public:
ListIterator(T* ptr) : ptr(ptr) {} ListIterator(Type* ptr) : ptr(ptr) {}
// I only implement the least necessary operators // I only implement the least necessary operators
ListIterator& operator++() { virtual ListIterator& operator++() {
this->ptr = this->ptr + 1; this->ptr = this->ptr + 1;
return *this; return *this;
} }
T* operator->() { Type* operator->() {
return this->ptr; return this->ptr;
} }
T& operator*() { Type& operator*() {
return *this->ptr; return *this->ptr;
} }
@ -46,15 +49,16 @@ public:
using Iterator = I; // Needed for range based for loop using Iterator = I; // Needed for range based for loop
protected: protected:
virtual Type* begin_ptr() = 0; virtual typename Iterator::Type* begin_ptr() = 0;
virtual Type* end_ptr() = 0; virtual typename Iterator::Type* end_ptr() = 0;
public: public:
Iterator begin() { return Iterator(this->begin_ptr()); } Iterator begin() { return Iterator(this->begin_ptr()); }
Iterator end() { return Iterator(this->end_ptr()); } Iterator end() { return Iterator(this->end_ptr()); }
virtual unsigned int insert(Type e) = 0;
virtual unsigned int insert_at(Type e, unsigned int i) = 0; virtual unsigned int insert_at(Type e, unsigned int i) = 0;
virtual unsigned int insert_first(Type e) = 0;
virtual unsigned int insert_last(Type e) = 0;
virtual Type remove_at(unsigned int i) = 0; virtual Type remove_at(unsigned int i) = 0;
virtual Type remove_first() = 0; virtual Type remove_first() = 0;