diff options
Diffstat (limited to 'backend/List.hpp')
-rw-r--r-- | backend/List.hpp | 110 |
1 files changed, 58 insertions, 52 deletions
diff --git a/backend/List.hpp b/backend/List.hpp index d6c7738..df075f9 100644 --- a/backend/List.hpp +++ b/backend/List.hpp @@ -1,66 +1,72 @@ #pragma once -#include <stdlib.h> +#include "List.h" template <typename T> -struct ListLink { - ListLink<T> * prev; - ListLink<T> * next; - T value; -}; +size_t List<T>::size() { + return this->length; +} -template<typename T> -class List { -public: - size_t size() { return this->length; } +template <typename T> +void List<T>::push_back(T el) { + ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); + link->prev = this->head; + link->next = nullptr; + link->value = el; + if (this->head != nullptr) this->head->next = link; + if (this->head == nullptr) this->tail = link; + this->head = link; + this->length++; +} - void push_back(T el) { - ListLink<T> * link = static_cast<ListLink<T> *>(malloc(sizeof(ListLink<T>))); - link->prev = this->head; - link->next = nullptr; - link->value = el; - if (this->head != nullptr) this->head->next = link; - if (this->head == nullptr) this->tail = link; - this->head = link; - this->length++; - } +template <typename T> +void List<T>::pop_back() { + if (this->head == nullptr) return; // empty list - void pop_back() { - if (this->head == nullptr) return; // empty list + ListLink<T> * secondlast = this->head->prev; + if (secondlast != nullptr) + secondlast->next = nullptr; + free(this->head); + if (this->tail == this->head) + this->tail = nullptr; + this->head = secondlast; - ListLink<T> * secondlast = this->head->prev; - if (secondlast != nullptr) - secondlast->next = nullptr; - free(this->head); - if (this->tail == this->head) - this->tail = nullptr; - this->head = secondlast; + this->length--; +} - this->length--; +template <typename T> +void List<T>::clear() { + ListLink<T> * link = this->tail; + while (link != nullptr) { + ListLink<T> * next = link->next; + free(link); + link = next; } + this->head = nullptr; + this->tail = nullptr; + this->length = 0; +} - void clear() { - ListLink<T> * link = this->tail; - while (link != nullptr) { - ListLink<T> * next = link->next; - free(link); - link = next; - } - this->head = nullptr; - this->tail = nullptr; - this->length = 0; - } +template <typename T> +T & List<T>::operator [] (size_t index) { + ListLink<T> * link = this->tail; + for (size_t i = 0; i < index; i++) + link = link->next; + return link->value; +} - T & operator [] (size_t index) { - ListLink<T> * link = this->tail; - for (size_t i = 0; i < index; i++) - link = link->next; - return link->value; - } +template <typename T> +ListRange<T> List<T>::range() { + return { *this }; +} -private: - ListLink<T> * head = nullptr; - ListLink<T> * tail = nullptr; - size_t length = 0; -}; +template <typename T> +ListIterator<T> List<T>::begin() { + return this->range().begin(); +} + +template <typename T> +ListIterator<T> List<T>::end() { + return this->range().end(); +} |