#ifndef LIST_H #define LIST_H #include namespace ecci { template class List { private: struct Node { public: Node* previous; DataType data; Node* next; public: Node(const DataType& data, Node* previous = nullptr, Node* next = nullptr) : previous(previous) , data{data} , next{next} { } }; private: size_t count; Node* first; Node* last; public: List() : count{0} , first{nullptr} , last{nullptr} { } ~List() { clear(); } inline bool isEmpty() const { return this->first == nullptr; } inline size_t getCount() const { return this->count; } void append(const DataType& element) { if ( isEmpty() ) this->first = this->last = new Node(element); else this->last = this->last->next = new Node(element, this->last); ++this->count; } void clear() { while ( ! this->isEmpty() ) this->removeFirst(); } void removeFirst() { Node* temp = this->first; this->first = this->first->next; delete temp; if ( this->first ) this->first->previous = nullptr; else this->last = nullptr; --this->count; } public: class Iterator { private: Node* current; public: explicit Iterator(Node* current = nullptr) : current(current) { } // itr1 != itr2 inline bool operator!=(const Iterator& other) const { return this->current != other.current; } // *itr inline DataType& operator*() { return this->current->data; } // ++itr inline Iterator& operator++() { this->current = this->current->next; return *this; } // itr++ inline Iterator operator++(int) { Iterator before( *this ); this->current = this->current->next; return before; } //operator-- }; public: inline Iterator begin() { return Iterator(this->first); } inline Iterator end() { return Iterator(nullptr); } }; } // namespace ecci #endif // LIST_H