Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
LinkedList.hpp
[詳解]
1// ----------------------------------------------------------------------------
6
7#pragma once
8
9#include "WindowsLibrary.h"
10
11namespace alt
12{
13 namespace skeleton
14 {
18 template<class T>
20 {
21 public:
24 {
25 _prev = nullptr;
26 _next = nullptr;
27 _value = nullptr;
28 };
29
32 LinkedListNode<T> (T& value)
34 {
35 _value = new T (value);
36 };
37
40 {
41 delete _value;
42 };
43
46
49
52 };
53
56 template<class T>
58 {
59 public:
62 {
63 _node = node;
64 };
65
68 {
69 _node = nullptr;
70 };
71
74 T& APIENTRY operator * ()
75 {
76 return *(_node->_value);
77 };
78
82 {
83 _node = _node->_next;
84 return *this;
85 };
86
92 bool APIENTRY operator != (const LinkedListIterator<T>& comp)
93 {
94 return _node != comp._node;
95 };
96
97 private:
98 LinkedListIterator<T> () = delete;
100 };
101
104 template<class T>
106 {
107 public:
110 {
111 _node = new LinkedListNode<T> ();
112 _size = 0;
113 };
114
117 LinkedList<T> (const LinkedList<T>& base)
118 : LinkedList<T> ()
119 {
120 operator =(base);
121 };
122
124 virtual APIENTRY ~LinkedList<T> ()
125 {
126 Clear ();
127 delete _node;
128 };
129
133 VOID APIENTRY AddFirst (T& item)
134 {
135 LinkedListNode<T>* newNode = new LinkedListNode<T> (item);
136
137 if (_size == 0)
138 {
139 _node->_prev = newNode;
140 _node->_next = newNode;
141 }
142 else
143 {
144 LinkedListNode<T>* next = _node->_next;
145 _node->_next = newNode;
146 newNode->_next = next;
147 next->_prev = newNode;
148 }
149
150 _size++;
151 };
152
156 VOID APIENTRY AddLast (T& item)
157 {
158 LinkedListNode<T>* newNode = new LinkedListNode<T> (item);
159
160 if (_size == 0)
161 {
162 _node->_prev = newNode;
163 _node->_next = newNode;
164 }
165 else
166 {
167 LinkedListNode<T>* prev = _node->_prev;
168 _node->_prev = newNode;
169 newNode->_prev = prev;
170 prev->_next = newNode;
171 }
172
173 _size += 1;
174 };
175
179 T* APIENTRY Get (SIZE_T index) const
180 {
181 LinkedListNode<T>* current = _node->_next;
182
183 for (INT i = 0; i < index; i++)
184 {
185 if (current == nullptr)
186 {
187 break;
188 }
189 current = current->_next;
190 }
191
192 return current != nullptr ? current->_value : nullptr;
193 };
194
198 T* APIENTRY operator[](SIZE_T index) const
199 {
200 return Get (index);
201 };
202
205 VOID APIENTRY operator = (const LinkedList<T>& base)
206 {
207 Clear ();
208
209 for (auto& itr : base)
210 {
211 AddLast (itr);
212 }
213 }
214
216 VOID APIENTRY RemoveFirst ()
217 {
218 if (_size == 0) return;
219
220 LinkedListNode<T>* node1 = _node->_next;
221 LinkedListNode<T>* node2 = node1->_next;
222 delete node1;
223 _node->_next = node2;
224
225 _size--;
226 };
227
229 VOID APIENTRY RemoveLast ()
230 {
231 if (_size == 0) return;
232
233 LinkedListNode<T>* node1 = _node->_prev;
234 LinkedListNode<T>* node2 = node1->_prev;
235 delete node1;
236 _node->_prev = node2;
237
238 _size--;
239 };
240
243 VOID APIENTRY Remove (T& item)
244 {
245 LinkedListNode<T>* current = _node->_next;
246 LinkedListNode<T>* before = _node;
247 LinkedListNode<T>* after = current->_next;
248
249 for (INT i = 0; i < _size; i++)
250 {
251 if (*(current->_value) == item)
252 {
253 break;
254 }
255
256 current = current->_next;
257 if (current == nullptr)
258 {
259 break;
260 }
261 before = current->_prev;
262 after = current->_next;
263 }
264
265 if (current != nullptr)
266 {
267 if (before != nullptr && before->_next != nullptr)
268 before->_next = after;
269 if (after != nullptr && after->_prev != nullptr)
270 after->_prev = before;
271
272 delete current;
273 _size--;
274 }
275 };
276
280 VOID APIENTRY Enqueue (T& item)
281 {
282 AddLast (item);
283 };
284
291 bool APIENTRY Dequeue (T& item)
292 {
293 bool ret = true;
294 T* value = Get (0);
295
296 if (value != nullptr) item = *value;
297 else ret = false;
298
299 RemoveFirst ();
300
301 return ret;
302 };
303
307 SIZE_T APIENTRY Size () const
308 {
309 return _size;
310 };
311
313 VOID APIENTRY Clear ()
314 {
315 while (_size > 0)
316 {
317 LinkedListNode<T>* node = _node->_next;
318 delete _node;
319 _node = node;
320 _size--;
321 }
322 };
323
326 LinkedListIterator<T> APIENTRY begin () const
327 {
328 return LinkedListIterator<T> (_node->_next);
329 };
330
333 LinkedListIterator<T> APIENTRY end () const
334 {
335 return LinkedListIterator<T> (nullptr);
336 };
337
340
341 private:
343 SIZE_T _size;
344 };
345 }
346}
WindowsAPIを集約したプロジェクトファイル
ポインタによる連結リストを具現したクラス
Definition: LinkedList.hpp:106
T *APIENTRY Get(SIZE_T index) const
指定したインデックスの値を取得
Definition: LinkedList.hpp:179
VOID APIENTRY RemoveLast()
LinkedList<T>の末尾アイテムを削除
Definition: LinkedList.hpp:229
VOID APIENTRY Remove(T &item)
LinkedList<T>から値を削除
Definition: LinkedList.hpp:243
VOID APIENTRY AddFirst(T &item)
LinkedList<T>へ値を追加
Definition: LinkedList.hpp:133
LinkedListIterator< T > APIENTRY begin() const
for(auto n : list){}を使用できるようにするインターフェース
Definition: LinkedList.hpp:326
bool APIENTRY Dequeue(T &item)
LinkedList<T>から値を取得
Definition: LinkedList.hpp:291
VOID APIENTRY operator=(const LinkedList< T > &base)
同じ型のLinkedList<T>をコピー
Definition: LinkedList.hpp:205
VOID APIENTRY RemoveFirst()
LinkedList<T>の先頭アイテムを削除
Definition: LinkedList.hpp:216
LinkedListIterator< T > APIENTRY end() const
for(auto n : list){}を使用できるようにするインターフェース
Definition: LinkedList.hpp:333
friend LinkedListIterator< T >
Iteratorからのアクセス権を解放
Definition: LinkedList.hpp:339
VOID APIENTRY Clear()
領域の開放
Definition: LinkedList.hpp:313
VOID APIENTRY AddLast(T &item)
LinkedList<T>へ値を追加
Definition: LinkedList.hpp:156
LinkedListNode< T > * _node
Definition: LinkedList.hpp:342
SIZE_T APIENTRY Size() const
サイズを取得
Definition: LinkedList.hpp:307
T *APIENTRY operator[](SIZE_T index) const
LinkedList<T>を配列操作で取得
Definition: LinkedList.hpp:198
VOID APIENTRY Enqueue(T &item)
LinkedList<T>へ値を追加
Definition: LinkedList.hpp:280
Node先頭〜末尾までをサーチするイテレータ
Definition: LinkedList.hpp:58
T &APIENTRY operator*()
データアクセス用オペレータ
Definition: LinkedList.hpp:74
LinkedListIterator< T > &APIENTRY operator++()
ノード移動用オペレータ
Definition: LinkedList.hpp:81
LinkedListNode< T > * _node
Definition: LinkedList.hpp:99
bool APIENTRY operator!=(const LinkedListIterator< T > &comp)
ノード末尾確認用オペレータ
Definition: LinkedList.hpp:92
双方向連結用ノードクラス
Definition: LinkedList.hpp:20
LinkedListNode< T > * _prev
先頭を保持するノード
Definition: LinkedList.hpp:45
T * _value
管理するデータ
Definition: LinkedList.hpp:51
LinkedListNode< T > * _next
末尾を保持するノード
Definition: LinkedList.hpp:48
Definition: DBLibrary.h:12