質問編集履歴

1

指摘事項に対応しました。

2019/06/29 16:22

投稿

idonotknow
idonotknow

スコア74

test CHANGED
File without changes
test CHANGED
@@ -237,3 +237,83 @@
237
237
  ↓こちらに実行可能なコードがあります
238
238
 
239
239
  [Wancobox](https://wandbox.org/permlink/ToGXLDVPamSUG3xj)
240
+
241
+
242
+
243
+ #追記
244
+
245
+ 生ポインタからunique_ptrに変更しました。
246
+
247
+ ```c++
248
+
249
+ template <typename Element> struct LinkedList
250
+
251
+ {
252
+
253
+ std::unique_ptr<LinkedList<Element>> next;
254
+
255
+ const Element value;
256
+
257
+
258
+
259
+ LinkedList(LinkedList<Element> next, Element value)
260
+
261
+ :next(new LinkedList<Element>(next)), value(value){}
262
+
263
+ LinkedList(std::unique_ptr<LinkedList<Element>> next, Element value)
264
+
265
+ :next(std::move(next)), value(value){}
266
+
267
+ };
268
+
269
+
270
+
271
+ template <typename Element> struct LLStack : Stack<Element>
272
+
273
+ {
274
+
275
+ LLStack(): head(nullptr){}
276
+
277
+
278
+
279
+ void push(Element ele)
280
+
281
+ {
282
+
283
+ head.reset(new LinkedList<Element>(std::move(head), ele));
284
+
285
+ }
286
+
287
+
288
+
289
+ std::optional<Element> pop()
290
+
291
+ {
292
+
293
+ if (head == nullptr)
294
+
295
+ {
296
+
297
+ return std::nullopt;
298
+
299
+ }
300
+
301
+ auto ele = head->value;
302
+
303
+ head = std::move(head->next);
304
+
305
+
306
+
307
+ return ele;
308
+
309
+ }
310
+
311
+
312
+
313
+ private:
314
+
315
+ std::unique_ptr<LinkedList<Element>> head;
316
+
317
+ };
318
+
319
+ ```