-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question3.h
48 lines (39 loc) · 899 Bytes
/
Question3.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Created by 张元一 on 16/02/2022.
//
#ifndef HW5_QUESTION3_H
#define HW5_QUESTION3_H
#include <memory>
#include <iostream>
#include <utility>
#include <cstddef>
template <typename T>
struct Node {
T data;
std::unique_ptr<Node> next;
Node(T data) : data{data}, next{nullptr} {}
};
template <typename T>
struct List {
List() : head{nullptr} {};
void push(T data) {
auto temp{std::make_unique<Node<T>>(data)};
if (head) {
temp->next = std::move(head);
head = std::move(temp);
} else {
head = std::move(temp);
}
}
T pop() {
if (head == nullptr) {
return NULL;
}
std::unique_ptr<Node<T>> temp = std::move(head);
head = std::move(temp->next);
return temp->data;
}
private:
std::unique_ptr<Node<T>> head;
};
#endif //HW5_QUESTION3_H