aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorymherklotz <ymherklotz@gmail.com>2016-12-09 22:32:32 +0000
committerymherklotz <ymherklotz@gmail.com>2016-12-09 22:32:32 +0000
commit6180812e7967ab37e0013c092c67a3c6bdcb7769 (patch)
treed0c2e71d3624ba3e726c9ad6c82a6e83ae68c542
parentd8c53d9d9ba4f275db25c3a98c6e1b380b3c69e1 (diff)
downloadA-star-algorithm-6180812e7967ab37e0013c092c67a3c6bdcb7769.tar.gz
A-star-algorithm-6180812e7967ab37e0013c092c67a3c6bdcb7769.zip
fixed Node class and removed destructor
-rwxr-xr-xbin/mainbin209120 -> 209408 bytes
-rw-r--r--include/astar.hpp4
-rw-r--r--include/priority_queue.hpp14
-rw-r--r--src/main.cpp14
4 files changed, 23 insertions, 9 deletions
diff --git a/bin/main b/bin/main
index 83298da..b593129 100755
--- a/bin/main
+++ b/bin/main
Binary files differ
diff --git a/include/astar.hpp b/include/astar.hpp
index c0eeee0..d40b97f 100644
--- a/include/astar.hpp
+++ b/include/astar.hpp
@@ -18,6 +18,10 @@ public:
// TODO These constructors have to change
Node();
+ void set_f(int i) {
+ f_score = i;
+ }
+
// overloading operators for ease of use.
friend bool operator<(const Node& n1, const Node& n2);
friend bool operator==(const Node& n1, const Node& n2);
diff --git a/include/priority_queue.hpp b/include/priority_queue.hpp
index f3b78ca..6fb4f54 100644
--- a/include/priority_queue.hpp
+++ b/include/priority_queue.hpp
@@ -36,14 +36,12 @@ template<typename T>
PriorityQueue<T>::PriorityQueue() : size(0), capacity(1) {
T *tmp_head = new T;
- std::cout << "constructor" << std::endl;
priority_array = tmp_head;
}
template<typename T>
PriorityQueue<T>::~PriorityQueue() {
- std::cout << "destructor" << std::endl;
delete[] priority_array;
}
@@ -57,8 +55,6 @@ void PriorityQueue<T>::push(const T& element) {
while(insert_loc < size && element > priority_array[insert_loc])
++insert_loc;
- size++;
-
insert_queue(element, insert_loc);
}
@@ -66,11 +62,9 @@ template<typename T>
T PriorityQueue<T>::pop() {
if(!size)
throw "Priority Queue is empty, no item to pop";
- else if(size - 1 == capacity / 2)
+ else if(size == capacity / 2)
resize_queue(0.5);
- size--;
-
return remove_queue(0);
}
@@ -90,7 +84,7 @@ template<typename T>
void PriorityQueue<T>::insert_queue(const T& element, const unsigned int& loc) {
T *tmp_array = new T[capacity];
- for(unsigned int i = 0; i < size; ++i)
+ for(unsigned int i = 0; i < size + 1; ++i)
if(i == loc)
tmp_array[i] = element;
else if(i < loc)
@@ -98,6 +92,8 @@ void PriorityQueue<T>::insert_queue(const T& element, const unsigned int& loc) {
else
tmp_array[i] = priority_array[i - 1];
+ size++;
+
delete[] priority_array;
priority_array = tmp_array;
}
@@ -116,6 +112,8 @@ T PriorityQueue<T>::remove_queue(const unsigned int& loc) {
else
tmp_array[i - 1] = priority_array[i];
+ size--;
+
delete[] priority_array;
priority_array = tmp_array;
diff --git a/src/main.cpp b/src/main.cpp
index e679134..3831e46 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -23,6 +23,13 @@ int main(int argc, char *argv[]) {
PriorityQueue<Node> pq;
Node n, n2, n3, n4, n5, n6;
+ n.set_f(65);
+ n2.set_f(2);
+ n3.set_f(6);
+ n4.set_f(34);
+ n5.set_f(75);
+ n6.set_f(2);
+
pq.push(n);
pq.push(n2);
pq.push(n3);
@@ -30,7 +37,12 @@ int main(int argc, char *argv[]) {
pq.push(n5);
pq.push(n6);
- cout << "Node in first queue: " << pq.pop() << endl;
+ cout << "First node in queue: " << pq.pop() << endl;
+ cout << "Second node in queue: " << pq.pop() << endl;
+ cout << "Third node in queue: " << pq.pop() << endl;
+ cout << "Fourth node in queue: " << pq.pop() << endl;
+ cout << "Fifth node in queue: " << pq.pop() << endl;
+ cout << "Sixth node in queue: " << pq.pop() << endl;
return 0;
}