From 6180812e7967ab37e0013c092c67a3c6bdcb7769 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Fri, 9 Dec 2016 22:32:32 +0000 Subject: fixed Node class and removed destructor --- bin/main | Bin 209120 -> 209408 bytes include/astar.hpp | 4 ++++ include/priority_queue.hpp | 14 ++++++-------- src/main.cpp | 14 +++++++++++++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/bin/main b/bin/main index 83298da..b593129 100755 Binary files a/bin/main and b/bin/main 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 PriorityQueue::PriorityQueue() : size(0), capacity(1) { T *tmp_head = new T; - std::cout << "constructor" << std::endl; priority_array = tmp_head; } template PriorityQueue::~PriorityQueue() { - std::cout << "destructor" << std::endl; delete[] priority_array; } @@ -57,8 +55,6 @@ void PriorityQueue::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 T PriorityQueue::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 void PriorityQueue::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::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::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 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; } -- cgit