-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.cpp
More file actions
86 lines (67 loc) · 1.76 KB
/
Path.cpp
File metadata and controls
86 lines (67 loc) · 1.76 KB
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
#include "Path.hpp"
#include <iostream>
// To initialize starting node path with weight 0
Path::Path( int node_name , double weight ) {
this->path = new int[50];
this->size = 50;
this->count = 1;
this->weight = weight;
for( int i{0}; i < this->size; i++ ) {
this->path[i] = 0;
}
this->path[0] = node_name;
}
// To constructing new path
Path::Path( int* previous_path , int previous_path_count , int current_node , double weight ) {
this->path = new int[50 + previous_path_count];
this->size = 50 + previous_path_count;
this->count = 1 + previous_path_count;
this->weight = weight;
for( int i{0}; i < this->size; i++ ) {
this->path[i] = 0;
}
for( int i{0}; i < previous_path_count; i++ ) {
this->path[i] = previous_path[i];
}
this->path[previous_path_count] = current_node;
}
Path::~Path() {
delete[] this->path;
}
void Path::add_node_to_path( int path ) {
if( this->count >= this->size - 3 ) {
this->resize();
}
this->path[this->count++] = path;
}
int Path::get_last_node() {
return this->path[this->count - 1];
}
int Path::get_count() {
return this->count;
}
int* Path::get_path() {
return this->path;
}
void Path::set_weight( double weight ) {
this->weight = weight;
}
double Path::get_weight() {
return this->weight;
}
void Path::resize() {
int new_size = this->size * 2;
int* new_array = new int[new_size]();
for( int i = 0; i < this->count; i++ ) {
new_array[i] = this->path[i];
}
delete[] this->path;
this->path = new_array;
this->size = new_size;
}
void Path::print() {
for( int i{0}; i < this->count; i++ ) {
std::cout << this->path[i] << " ";
}
std::cout << std::endl;
}