You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB

2 years ago
/*/*******************************************************************************
** **
** Jiedi(China nanjing)Ltd. **
** **
*******************************************************************************/
/*****************************FILE INFOMATION***********************************
**
** Project : ---leetcode
** Contact : xiacaojun@qq.com
** : http://blog.csdn.net/jiedichina
** : http://study.163.com/u/xiacaojun
https://jiedi.ke.qq.com/
csdn https://edu.csdn.net/course/detail/25037
** 51cto http://edu.51cto.com/lecturer/index/user_id-100013755.html
** http://www.laoxiaketang.com
**
** ---leetcode 296249312
** : jiedi2007
** :
**
*****************************************************************************
// 算法设计与编程实践---基于leetcode的企业真题库 课程 QQ群296249312 下载代码和交流*/
/*
* @lc app=leetcode.cn id=225 lang=cpp
*
* [225]
*/
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
queue<int> temp;//建立一个辅助队列
while(q.empty() == false){
//将队列中的所有元素都存入辅助队列中
temp.push(q.front());
q.pop();
}
q.push(x);//向队列中压入元素
while(temp.empty() == false){
//逐个将辅助队列中哦元素送回队列q中
q.push(temp.front());
temp.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
//将队首元素返回
int a = q.front();
q.pop();
return a;
}
/** Get the top element. */
int top() {
return q.front();//直接拿队首元素
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
private:
queue<int> q;
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/