-
Notifications
You must be signed in to change notification settings - Fork 1
/
vertex_list.cpp
152 lines (135 loc) · 2.96 KB
/
vertex_list.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
#include "vertex_list.h"
namespace finding_max_flow
{
vertex_list::vertex_list() : m_head(nullptr), m_tail(nullptr), m_size(0)
{
}
vertex_node* vertex_list::make_new_node(const int vertex, const int dest, const int i_weight_to_next, vertex_node* prev, vertex_node* next)
{
const auto newNode = new vertex_node(vertex, dest, i_weight_to_next);
newNode->set_node_ptr(prev, next);
return newNode;
}
void vertex_list::make_empty_list()
{
m_head = m_tail = nullptr;
m_size = 0;
}
void vertex_list::add_to_head(vertex_node* i_new_node)
{
i_new_node->m_next = m_head;
i_new_node->m_prev = nullptr;
if (!is_empty()) {
m_head->m_prev = i_new_node;
}
if (m_tail == nullptr) {
m_tail = i_new_node;
}
m_head = i_new_node;
m_size++;
}
vertex_node* vertex_list::remove_from_tail()
{
vertex_node* node = nullptr;
if (m_head == nullptr)
{
return node;
}
else
{
node = m_tail;
if (m_head != m_tail)
{
m_tail = m_tail->m_prev;
m_tail->m_next = nullptr;
}
else
{
m_head = m_tail = nullptr;
}
m_size--;
return node;
}
}
vertex_node* vertex_list::get_node_by_data(const int i_vertex_num) const
{
vertex_node* res = nullptr;
vertex_node* current = m_head;
while (current != nullptr) {
if (current->m_edge->get_y() == i_vertex_num)
{
res = current;
break;
}
current = current->m_next;
}
return res;
}
bool vertex_list::is_in_list(const int i_vertex_num) const
{
bool result = false;
vertex_node* temp = get_node_by_data(i_vertex_num);
if (temp != nullptr)
result = true;
return result;
}
bool vertex_list::remove_from_list(const int i_vertex_num)
{
bool res = false;
vertex_node* temp = get_node_by_data(i_vertex_num);
if (temp == m_tail) {
temp = remove_from_tail();
res = true;
}
else if (temp == m_head)
{
m_head = m_head->m_next;
m_head->m_prev = nullptr;
res = true;
}
else {
if (temp->m_next != nullptr)
{
temp->m_next->m_prev = temp->m_prev;
}
temp->m_prev->m_next = temp->m_next;
res = true;
}
m_size--;
delete temp;
return res;
}
int vertex_list::add_to_list(const int i_vertex_num, const int i_dest, const int i_weight_to_next)
{
constexpr int res = 1;
vertex_node* newNode = make_new_node(i_vertex_num, i_dest, i_weight_to_next);
add_to_head(newNode);
return res;
}
int vertex_list::add_to_list(vertex_node* i_to_add)
{
constexpr int res = 1;
add_to_head(i_to_add);
return res;
}
void vertex_list::print() const
{
vertex_node* current = m_head;
while (current != nullptr)
{
current->m_edge->print();
current = current->m_next;
}
}
void vertex_list::free_list()
{
vertex_node* node;
while (m_head != nullptr) {
node = m_head;
m_head = node->m_next;
delete node;
}
m_size = 0;
m_head = m_tail = nullptr;
}
}