class MyQueue {
public:
MyQueue() {
// do intialization if necessary
}
/*
* @param element: An integer
* @return: nothing
*/
void push(int element) {
// write your code here
if (m_s.empty())
{
First=element;
}
m_s.push(element);
}
/*
* @return: An integer
*/
int pop() {
// write your code here
int result=First;
stack<int> temp;
while(!m_s.empty())
{
temp.push(m_s.top());
m_s.pop();
}
temp.pop();//犯蠢了,如果temp弹出栈顶元素后为空,top操作会出错;
if (!temp.empty())//所以这里一定要加判空;
{
First=temp.top();
}
while(!temp.empty())
{
m_s.push(temp.top());
temp.pop();
}
return result;
}
/*
* @return: An integer
*/
int top() {
// write your code here
return First;
}
int First; //存储队列头;
stack<int> m_s;//存储元素;
};