|
4 | 4 |
|
5 | 5 | #pragma once
|
6 | 6 |
|
7 |
| -template <class T> |
8 |
| -class Node { |
9 |
| -public: |
10 |
| - Node(T d) { |
11 |
| - data = d; |
12 |
| - } |
13 |
| - |
14 |
| - T data; |
15 |
| - Node* last; |
16 |
| - Node* next; |
17 |
| -}; |
18 |
| - |
19 |
| -template <class T> |
20 |
| -class Deque { |
21 |
| -public: |
22 |
| - Deque() {} |
23 |
| - |
24 |
| - void push_back(T d) { |
25 |
| - if (!rootFront) { |
26 |
| - rootFront = new Node<T>(d); |
27 |
| - rootEnd = rootFront; |
| 7 | +namespace Deque { |
| 8 | + template<class T> |
| 9 | + class Node { |
| 10 | + public: |
| 11 | + Node(T d) { |
| 12 | + data = d; |
28 | 13 | }
|
29 |
| - else { |
30 |
| - Node<T>* newNode = new Node<T>(d); |
31 |
| - rootEnd.next = newNode; |
32 |
| - newNode->last = rootEnd; |
33 |
| - rootEnd = newNode; |
34 |
| - } |
35 |
| - } |
36 | 14 |
|
37 |
| - void push_front(T d) { |
38 |
| - if (!rootFront) { |
39 |
| - rootFront = new Node<T>(d); |
40 |
| - rootEnd = rootFront; |
41 |
| - } |
42 |
| - else { |
43 |
| - Node<T>* newNode = new Node<T>(d); |
44 |
| - newNode->next = rootFront; |
45 |
| - rootFront->last = newNode; |
46 |
| - rootFront = newNode; |
47 |
| - } |
48 |
| - } |
49 |
| - |
50 |
| - T pop_back() { |
51 |
| - if( rootEnd == nullptr) return nullptr; |
52 |
| - Node<T>* result = rootEnd; |
53 |
| - if ( rootEnd->last == nullptr ) { |
54 |
| - rootFront = nullptr; |
55 |
| - rootEnd = nullptr; |
| 15 | + T data; |
| 16 | + Node *last; |
| 17 | + Node *next; |
| 18 | + }; |
| 19 | + |
| 20 | + template<class T> |
| 21 | + class Deque { |
| 22 | + public: |
| 23 | + Deque() {} |
| 24 | + |
| 25 | + void push_back(T d) { |
| 26 | + if (!rootFront) { |
| 27 | + rootFront = new Node<T>(d); |
| 28 | + rootEnd = rootFront; |
| 29 | + } else { |
| 30 | + Node<T> *newNode = new Node<T>(d); |
| 31 | + rootEnd.next = newNode; |
| 32 | + newNode->last = rootEnd; |
| 33 | + rootEnd = newNode; |
| 34 | + } |
56 | 35 | }
|
57 |
| - else { |
58 |
| - rootEnd->last->next = nullptr; |
59 |
| - rootEnd = rootEnd->last; |
| 36 | + |
| 37 | + void push_front(T d) { |
| 38 | + if (!rootFront) { |
| 39 | + rootFront = new Node<T>(d); |
| 40 | + rootEnd = rootFront; |
| 41 | + } else { |
| 42 | + Node<T> *newNode = new Node<T>(d); |
| 43 | + newNode->next = rootFront; |
| 44 | + rootFront->last = newNode; |
| 45 | + rootFront = newNode; |
| 46 | + } |
60 | 47 | }
|
61 |
| - return result->data; |
62 |
| - } |
63 |
| - |
64 |
| - T pop_front() { |
65 |
| - if( rootFront == nullptr) return nullptr; |
66 |
| - Node<T>* result = rootFront; |
67 |
| - if ( rootFront->next == nullptr ) { |
68 |
| - rootFront = nullptr; |
69 |
| - rootEnd = nullptr; |
| 48 | + |
| 49 | + T pop_back() { |
| 50 | + if (rootEnd == nullptr) return nullptr; |
| 51 | + Node<T> *result = rootEnd; |
| 52 | + if (rootEnd->last == nullptr) { |
| 53 | + rootFront = nullptr; |
| 54 | + rootEnd = nullptr; |
| 55 | + } else { |
| 56 | + rootEnd->last->next = nullptr; |
| 57 | + rootEnd = rootEnd->last; |
| 58 | + } |
| 59 | + return result->data; |
70 | 60 | }
|
71 |
| - else { |
72 |
| - rootFront->next->last = nullptr; |
73 |
| - rootFront = rootFront->next; |
| 61 | + |
| 62 | + T pop_front() { |
| 63 | + if (rootFront == nullptr) return nullptr; |
| 64 | + Node<T> *result = rootFront; |
| 65 | + if (rootFront->next == nullptr) { |
| 66 | + rootFront = nullptr; |
| 67 | + rootEnd = nullptr; |
| 68 | + } else { |
| 69 | + rootFront->next->last = nullptr; |
| 70 | + rootFront = rootFront->next; |
| 71 | + } |
| 72 | + return result->data; |
74 | 73 | }
|
75 |
| - return result->data; |
76 |
| - } |
77 | 74 |
|
| 75 | + // https://gist.github.com/jeetsukumaran/307264 |
| 76 | + // TODO fix error |
| 77 | + class iterator { |
| 78 | + public: |
| 79 | + iterator(Node<T> *ptr) : m_Ptr(ptr) {} |
| 80 | + |
| 81 | + iterator operator++() { |
| 82 | + iterator i = *this; |
| 83 | + m_Ptr = m_Ptr->next; |
| 84 | + return i; |
| 85 | + } |
| 86 | + |
| 87 | + bool operator==( const iterator& other ) { |
| 88 | + return m_Ptr == other.m_Ptr; |
| 89 | + } |
| 90 | + bool operator!=( const iterator& other ) { |
| 91 | + return m_Ptr != other.m_Ptr; |
| 92 | + } |
| 93 | + Node<T>& operator*() { |
| 94 | + return **m_Ptr; |
| 95 | + } |
| 96 | + |
| 97 | + private: |
| 98 | + Node<T> *m_Ptr; |
| 99 | + }; |
78 | 100 |
|
79 |
| - Node<T>* rootFront = nullptr; |
80 |
| - Node<T>* rootEnd = nullptr; |
81 |
| -}; |
| 101 | + iterator begin() { |
| 102 | + if (rootFront == nullptr) return nullptr; |
| 103 | + return iterator(rootFront); |
| 104 | + } |
| 105 | + |
| 106 | + iterator end() { |
| 107 | + return iterator(nullptr); |
| 108 | + } |
82 | 109 |
|
| 110 | + Node<T> *rootFront = nullptr; |
| 111 | + Node<T> *rootEnd = nullptr; |
| 112 | + }; |
| 113 | +} |
83 | 114 |
|
0 commit comments