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
|
#include "minQ.h"
#include <iostream>
minQ::minQ(void)
{
}
minQ::~minQ(void)
{
}
int minQ::push(int num)
{
this->q.push(num);
while(this->min.size() > 0 && num < this->min.back()) {
this->min.pop_back();
}
this->min.push_back(num);
return(this->q.size());
}
int minQ::pop()
{
if(this->q.size() > 0) {
int front = this->q.front();
this->q.pop();
if(this->min.front() == front) {
this->min.pop_front();
}
} else {
cout << "empty que" << endl;
}
return(this->q.size());
}
int minQ::front()
{
return(this->q.front());
}
int minQ::getMin()
{
return(this->min.front());
}
void testMinQ()
{
minQ q;
q.push(2);
cout << q.getMin() << endl; // 2
q.push(1);
cout << q.getMin() << endl; // 1
q.push(3);
q.pop();
cout << q.getMin() << endl; // 1
q.push(0);
q.pop();
cout << q.getMin() << endl; // 0
}
|