-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.h
82 lines (69 loc) · 1.58 KB
/
Node.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
template<typename T>
class Node {
public:
Node();
explicit Node(const T& data, Node<T>* next = nullptr, Node<T>* prev = nullptr);
Node(const Node<T>& other);
Node(Node<T>&& other);
~Node();
Node<T>& operator=(const Node<T>& other);
Node<T>& operator=(Node<T>&& other);
T* data;
Node<T>* next;
Node<T>* prev;
private:
void reset();
};
//--------------------------------------------------------
template<typename T>
Node<T>::Node()
: data{ nullptr }, next{ nullptr }, prev{ nullptr } {}
template<typename T>
Node<T>::Node(const T& data, Node<T>* next, Node<T>* prev)
: data{ new T(data) }, next{ next }, prev{ prev } {}
template<typename T>
Node<T>::Node(const Node<T>& other)
: data{ (other.data == nullptr) ? nullptr : new T(*other.data) }, next{ other.next }, prev{ other.prev } {}
template<typename T>
Node<T>::Node(Node<T>&& other)
: data{ other.data }, next{ other.next }, prev{ other.prev }
{
other.reset();
}
template<typename T>
Node<T>::~Node()
{
delete data;
reset();
}
template<typename T>
Node<T>& Node<T>::operator=(const Node<T>& other)
{
if (this != &other) {
delete data;
data = (other.data == nullptr) ? nullptr : new T(*other.data);
next = other.next;
prev = other.prev;
}
return *this;
}
template<typename T>
Node<T>& Node<T>::operator=(Node<T>&& other)
{
if (this != &other) {
delete data;
data = other.data;
next = other.next;
prev = other.prev;
other.reset();
}
return *this;
}
template<typename T>
void Node<T>::reset()
{
data = nullptr;
next = prev = nullptr;
}
//--------------------------------------------------------