Published by
May 5, 2012 (last update: Mar 11, 2013)

priority_queue

Score: 3.3/5 (29 votes)
*****
look at this first :
http://www.cplusplus.com/reference/stl/priority_queue/

now we want to define a priority queue that enable us to Add element to priority queue with custom KEY.


Define Priority Queue



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
#include<vector>
using namespace std;
template <class T=int> 
class PriorityQueue
{
private:
	vector<int> Key;
	vector<T> element;
	vector<int>::iterator it;

	
	void insert(const T &value,const int &key ,const int l ,const int h)
	{
		if( h<=l )
		{
			if(Key.at(l)>= key )
			{
				it = element.begin();
				//it++;
				element.insert(it+l,value);

				it = Key.begin();
				//it++;
				Key.insert(it+l,key);
			}
			else
			{
				it = element.begin();
				it++;
				element.insert(it+l,value);

				it = Key.begin();
				it++;
				Key.insert(it+l,key);
			}
		}
		else
		{
			int mid = ( l + h ) / 2 ;

			if ( Key.at( mid ) == key )	// maybe queue have two element with same Priority 
			{
				it = element.begin();
				//it++;
				element.insert(it+mid,value);

				it = Key.begin();
				//it++;
				Key.insert(it+mid,key);
			} 
		    else if( Key.at( mid ) > key )
			{
				insert(value,key,l,mid-1 );
			}
			else if	( Key.at( mid ) < key )
			{
				insert(value,key,mid+1,h );
				
			}
			
		}
		
	}
public:
	PriorityQueue(const PriorityQueue& Copy)
	{
		Key = Copy.Key;
		element = Copy.element;
		
	}
	PriorityQueue operator=(const PriorityQueue& Copy)
	{
		Key = Copy.Key;
		element = Copy.element;
		return *this;
	}
	PriorityQueue()
	{
	}
	bool empty()
	{
		return element.empty();
	}
	int size()
	{
		return element.size();
	}
	void push(const T& value ,const int& key)
	{
		if(element.empty())
		{
			element.push_back(value);
			Key.push_back(key);
		}
		else
		{
			int s= element.size()-1;
			insert(value,key,0,s);
		}
	}
	T popLowestPriority()
	{
		
		T temp = element.at(0);

		it = element.begin();
		element.erase(it);

		it = Key.begin();
		Key.erase(it);

		return temp ;
	}
	T popHighestPriority()
	{
		
		T temp = element.back();
		element.pop_back();

		Key.pop_back();

		return temp ;
	}
	void changePriority(T value  , int newKey)
	{
		it = element.begin();
		int elementSize = element.size();
		for( int i = 0 ; i < elementSize ; i++ )
		{
			if( element.at( i ) == value )
			{
				element.erase( it+i);

				it = Key.begin();
				Key.erase(it+i);

				this->push(value,newKey);
			}
		}
	}
};	



Example :


Input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

	PriorityQueue<int> elem;
	elem.push(1,7);
	elem.push(2,1);
	elem.push(3,9);
	elem.push(4,3);
	elem.push(5,6);
	elem.push(8,6);
	
	for( int i = 0 ; !elem.empty() ; i++ )
		cout<<elem.popLowestPriority()<<endl;
	cout<<"-------"<<endl;
	elem.push(1,7);
	elem.push(2,1);
	elem.push(3,9);
	elem.push(4,3);
	elem.push(5,6);
	elem.push(8,6);

	elem.changePriority(8,0);
	for( int i = 0 ; !elem.empty() ; i++ )
		cout<<elem.popLowestPriority()<<endl;


Output :

2
4
8
5
1
3
-------
8
2
4
5
1
3



-----
Sincerely yours,
Sohrab Aboozarkhani Fard
B.S. Student,
Department of Computer Science, "Kharazmi" University , Tehran , Iran.