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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*/*******************************************************************************
** **
** 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();
*/