diff options
Diffstat (limited to 'List.hpp')
| -rw-r--r-- | List.hpp | 71 |
1 files changed, 39 insertions, 32 deletions
@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/01 09:31:07 by cacharle #+# #+# */ -/* Updated: 2020/04/15 12:57:58 by charles ### ########.fr */ +/* Updated: 2020/04/24 19:16:46 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,11 +34,10 @@ namespace ft explicit List(const allocator_type& alloc = allocator_type()) : m_front(NULL), m_back(NULL), m_size(0) {} - List(const List& other) { - *this = other; - } + List(const List& other) { *this = other; } - List& operator=(const List& other) { + List& operator=(const List& other) + { if (*this == other) return *this; delete m_front; @@ -67,19 +66,22 @@ namespace ft public: ListNode() : m_next(NULL), m_prev(NULL), m_data(value_type()) {} - ListNode(const ListNode& other) { + ListNode(const ListNode& other) + { if (*this != other) *this = other; } - ListNode& operator=(const ListNode& other) { + ListNode& operator=(const ListNode& other) + { m_next = other.m_next; m_prev = other.m_prev; m_data = other.m_data; return *this; } - ~ListNode() { + ~ListNode() + { if (m_next != NULL) { m_next->m_prev = NULL; delete m_next; @@ -90,16 +92,16 @@ namespace ft } } - ListNode(T& data) : m_next(NULL), m_prev(NULL), m_data(data) {} + ListNode(value_type& data) : m_next(NULL), m_prev(NULL), m_data(data) {} - ListNode* getNext() const { return m_next; } - ListNode* getPrev() const { return m_prev; } - T& getData() const { return m_data; } + ListNode* getNext() const { return m_next; } + ListNode* getPrev() const { return m_prev; } + value_type& getData() const { return m_data; } private: - ListNode* m_next; - ListNode* m_prev; - T m_data; + ListNode* m_next; + ListNode* m_prev; + value_type m_data; }; public: @@ -121,30 +123,30 @@ namespace ft ~iterator() {} - iterator(ListNode* current) : m_current(current) {} - - T& operator*() { - return m_current->getData(); - } + value_type& operator*() { return m_current->getData(); } + value_type& operator->() { return m_current->getData(); } - bool operator==(const iterator& other) { - return m_current == other.m_current; - } + bool operator==(const iterator& other) { return m_current == other.m_current; } bool operator!=(const iterator& other) { return !(operator==(other)); } - iterator& operator++(int) { // post + iterator& operator++(int) + { m_current = m_current->getNext(); return *this; } - iterator& operator++() { return operator++; } - iterator& operator--(int) { + iterator& operator--(int) + { m_current = m_current->getPrev(); return *this; } - iterator& operator--() { return operator++; } + + iterator& operator++() { return operator++; } + iterator& operator--() { return operator--; } private: + iterator(ListNode* current) : m_current(current) {} + List::ListNode* m_current; }; @@ -158,35 +160,40 @@ namespace ft reference front() { return m_front->getData(); } reference back() { return m_back->getData(); } - void push_front(const value_type& val) { + void push_front(const value_type& val) + { ListNode* new_front = new ListNode(val); new_front->setNext(m_front); m_front->setPrev(new_front); m_front = new_front; } - void push_back(const value_type& val) { + void push_back(const value_type& val) + { ListNode* new_back = new ListNode(val); new_back->setPrev(m_back); m_back->setNext(new_back); m_back = new_back; } - void pop_front() { + void pop_front() + { ListNode* tmp = m_front->getNext(); tmp->setPrev(NULL); delete m_front; m_front = tmp; } - void pop_back() { + void pop_back() + { ListNode* tmp = m_back->getPrev(); tmp->setNext(NULL); delete m_back; m_back = tmp; } - void clear() { + void clear() + { delete m_front; m_front = NULL; m_back = NULL; |
