-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue.cpp
81 lines (55 loc) · 1.86 KB
/
priority_queue.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
#include <queue>
#include <iostream>
#include "graph.h"
using namespace std;
/*****************************************************************************************
* priority queue
****************************************************************************************/
struct NodeCompare
{
//tests whether n1 worse than n2
bool operator()(const Node *n1, const Node *n2) const{
return n1->dist > n2->dist;
}
};
//changes the key of node in a priority queue
priority_queue<Node*,vector<Node*>,NodeCompare> change_key(priority_queue<Node*,vector<Node*>,NodeCompare> *old_queue, Node *n, int new_key) {
priority_queue<Node*,vector<Node*>,NodeCompare> new_queue;
while(!old_queue->empty()) {
Node *k = old_queue->top();
old_queue->pop();
if(n == k) k->dist = new_key;
new_queue.push(k);
}
return new_queue;
}
void test_queue(){
priority_queue<Node*,vector<Node*>,NodeCompare> node_queue;
//create new nodes
Node a(1,30);
Node b(2,20);
Node c(3,10);
cout << "size of queue " << node_queue.size() << endl;
node_queue.push(&a);
cout << "size of queue " << node_queue.size() << endl;
node_queue.push(&b);
cout << "size of queue " << node_queue.size() << endl;
node_queue.push(&c);
cout << "size of queue " << node_queue.size() << endl;
node_queue = change_key(&node_queue, &a, 4);
Node *n = node_queue.top();
cout << "popped from queue:" << endl;
n->print();
node_queue.pop();
Node *m = node_queue.top();
cout << "popped from queue:" << endl;
m->print();
node_queue.pop();
Node *k = node_queue.top();
cout << "popped from queue:" << endl;
k->print();
node_queue.pop();
}
/*****************************************************************************************
* end
****************************************************************************************/