-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cpp
153 lines (129 loc) · 4.35 KB
/
Node.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Node.h"
/*****************************************************************************
Constructors/Destructor
*****************************************************************************/
// Time Complexity: O(1)
template <class T>
Node<T>::Node() : data_(T()) {}
// Time Complexity: O(1)
template <class T>
Node<T>::Node(const T& data) : data_(data) {}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
Node<T>::Node(const T& data, const std::list<Node<T>*>& edge_list) : data_(data) {
copy_(edge_list);
}
// Time Complexity: O(N), where N is the number of edges in the edge list in other
template <class T>
Node<T>::Node(const Node<T>& other) : data_(other.data_) {
copy_(other.edge_list_);
}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
Node<T>::~Node() {
edge_list_.clear();
}
/*****************************************************************************
Operator Overload
*****************************************************************************/
// Time Complexity: O(N), where N is the number of edges in the edge list in other
template <class T>
T& Node<T>::operator=(const Node<T>& other) {
data_ = other.data_;
copy_(other.edge_list_);
}
/*****************************************************************************
Data Manipulation
*****************************************************************************/
// Time Complexity: O(1)
template <class T>
void Node<T>::setData(const T& data) {
data_ = data;
}
// Time Complexity: O(1)
template <class T>
void Node<T>::setEdgeList(std::list<Node<T>*> edge_list) {
edge_list_ = edge_list;
}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
void Node<T>::addEdge(Node<T>*& node) {
if (contains(node)) {
return;
}
edge_list_.push_back(node);
}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
void Node<T>::removeEdge(Node<T>*& node) {
if (!contains(node)) {
return;
}
typename std::list<Node<T>*>::iterator it = search_(node);
edge_list_.erase(it); //Erase via iterator;
}
/*****************************************************************************
Data Observation
*****************************************************************************/
// Time Complexity: O(1)
template <class T>
T Node<T>::getData() const {
return data_;
}
// Time Complexity: O(1)
template <class T>
std::list<Node<T>*> Node<T>::getEdgeList() const {
return edge_list_;
}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
bool Node<T>::contains(Node<T>*& node) const {
if (empty()) {
return false;
}
for (typename std::list<Node<T>*>::const_iterator it = edge_list_.cbegin(); it != edge_list_.cend(); ++ it) {
if ((*it)->getData() == node->getData()) {
return true;
}
}
return false;
}
// Time Complexity: O(1)
template <class T>
bool Node<T>::empty() const {
return edge_list_.empty();
}
// Time Complexity: O(1)
template <class T>
int Node<T>::size() const {
return edge_list_.size();
}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
void Node<T>::printEdgeList() const {
for (typename std::list<Node<T>*>::const_iterator it = edge_list_.cbegin(); it != edge_list_.cend(); ++it) {
std::cout << (*it)->getData() << ", ";
}
std::cout << '\n';
}
/*****************************************************************************
Private Functions
*****************************************************************************/
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
void Node<T>::copy_(const std::list<Node<T>*>& edge_list) {
for (typename std::list<Node<T>*>::const_iterator it = edge_list.cbegin(); it != edge_list.cend(); ++it) {
edge_list_.push_back((*it));
}
}
// Time Complexity: O(N), where N is the number of edges in the edge list
template <class T>
typename std::list<Node<T>*>::iterator Node<T>::search_(Node<T>*& node) {
typename std::list<Node<T>*>::iterator it = edge_list_.begin();
for (; it != edge_list_.end(); ++it) {
if ((*it)->getData() == node->getData()) {
return it;
}
}
return it;
}