总述:
本代码为之前文章的代码的总结和优化版本,供大家学习参考,当然,本人代码能力并不是很强,请尽量看吧。
代码总述
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
class Getmax
{
/**
* 此类的方法如下:
* 即得到的stack.top()即为结果,即为最大值的所求
**/
public:
void pushStack(int number)
{
stack1.push(number);
if (stack2.empty())
stack2.push(number);
else if (number >= stack2.top())
stack2.push(number);
}
int popStack()
{
int num1;
if (stack1.top() == stack2.top())
{
stack1.pop();
num1 = stack2.top();
stack2.pop();
}
else
{
num1 = stack1.top();
stack1.pop();
}
return num1;
}
private:
stack<int> stack1, stack2;
};
class Getmin
{
/**
* 此类的方法如下:
* 即得到的stack.top()即为结果,即为最小值的所求
**/
public:
void pushStack(int number)
{
stack1.push(number);
if (stack2.empty())
stack2.push(number);
else if (number <= stack2.top())
stack2.push(number);
}
int popStack()
{
int num1;
if (stack1.top() == stack2.top())
{
stack1.pop();
num1 = stack2.top();
stack2.pop();
}
else
{
num1 = stack1.top();
stack1.pop();
}
return num1;
}
private:
stack<int> stack1, stack2;
};
class QueueToStack
{
private:
queue<int> Date, Help;
public:
void QueueToStack_push(int x)
{
/**
*我不能确定返回的是那个队列,
* 但是我一定确定的是一定是导致后的队列的队列的顶端即为所求
**/
if (!Date.empty())
{
Help.push(x);
while (!Date.empty())
{
Help.push(Date.front());
Date.pop();
}
}
else
{
Date.push(x);
while (!Help.empty())
{
Date.push(Help.front());
Help.pop();
}
}
}
void QueueToStack_pop()
{
if (!Date.empty() && Help.empty())
cout << "The Stack is empty." << endl;
else if (!Date.empty())
Date.pop()