In: Computer Science
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class MinQueue {
public:
queue<T> Q;
deque<T> D;
void enque_element(T element)
{
if (Q.size() == 0) {
Q.push(element);
D.push_back(element);
}
else {
Q.push(element);
while
(!D.empty() &&
D.back() >
element) {
D.pop_back();
}
D.push_back(element);
}
}
void deque_element()
{
if (Q.front() == D.front()) {
Q.pop();
D.pop_front();
}
else {
Q.pop();
}
}
T getMin()
{
return D.front();
}
};
int main()
{
MinQueue<int> k;
int sample[10] = {60,98,110,55,34,333,444,67,89,450
};
for (int i = 0; i < 10; i++) {
k.enque_element(sample[i]);
}
k.deque_element();
cout << k.getMin() << "\n";
}