1、栈与队列理论基础
队列是先进先出,栈是先进后出
C++中stack是容器么?
我们使用的stack是属于哪个版本的STL?
我们使用的STL中stack是如何实现的?
stack 提供迭代器来遍历stack空间么?
首先大家要知道栈和队列是STL(C++标准库)里面的两个数据结构(下面介绍的是SGI STL里面的数据结构)
栈提供push和pop等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。不像是set或者map提供迭代器iterator来遍历所有元素
栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)
所以STL中栈往往不被归类为容器,而被归类为container adapter(容器适配器)
从下图中可以看出,栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的,主要就是数组和链表的底层实现
SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构
deque是一个双向队列,只要封住一段,只开通另一端就可以实现栈的逻辑了
指定vector为栈的底层实现,初始化语句如下
std::stack<int, std::vector<int> > third; // 使用vector为底层容器的栈
刚刚讲过栈的特性,对应的队列的情况是一样的
队列中先进先出的数据结构,同样不允许有遍历行为,不提供迭代器, SGI STL中队列一样是以deque为缺省情况下的底部结构
指定list 为起底层实现,初始化queue的语句如下
std::queue<int, std::list<int>> third; // 定义以list为底层容器的队列
STL 队列也不被归类为容器,而被归类为container adapter(容器适配器)
2、栈与队列的应用
2.1 leetcode 232:用栈实现队列
第一遍代码,以栈底为队列的开头,栈顶为队列的结尾,用两个栈实现插入和获取开头元素,其中一个栈作为辅助,在插入和获取开头元素时,记录除目标元素之外的其他元素
全局变量写在最前面public里面,stack/queue的pop()没有返回值,要top()先记录下来
class MyQueue {
public:
stack<int> target;//全局变量写在最前面
stack<int> tmp;
MyQueue() {
}
void push(int x) {
target.push(x);
}
int pop() {
while(!target.empty()) {
int tmp1 = target.top();//pop()没有返回值要top()先记录下来
tmp.push(tmp1);
target.pop();
}
int result = tmp.top();
tmp.pop();
while(!tmp.empty()) {
target.push(tmp.top());
tmp.pop();
}
return result;
}
int peek() {
while(!target.empty()) {
int tmp1 = target.top();
tmp.push(tmp1);
target.pop();
}
int result = tmp.top();
while(!tmp.empty()) {
target.push(tmp.top());
tmp.pop();
}
return result;
}
bool empty() {
return target.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈一个输入栈,一个输出栈(因为把栈内元素压入另一个栈顺序会改变),这里要注意输入栈和输出栈的关系,而非把一个当作队列辅助(第一次代码),这样pop的时候不用把弹入另一个栈的所有元素压回去了
在push数据的时候,只要数据放进输入栈就好,但在pop的时候,操作就复杂一些
1、输出栈如果为空,就把输入栈数据全部导入进来(注意是全部导入),再从出栈弹出数据
2、只要输出栈不为空,就不能再从输入栈导数据了,直接从输出栈弹出数据就可以了
如果进栈和出栈都为空的话,说明模拟的队列为空了
int res = this->pop(); // 直接使用已有的pop函数
代码随想录整体代码
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
if (stOut.empty()) {
// 从stIn导入数据直到stIn为空
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();
}
};
2.2 注意函数复用
在工业级别代码开发中,最忌讳的就是实现一个类似的函数,直接把代码粘过来改一改就完事了,一定要懂得复用,功能相近的函数要抽象出来,不要大量的复制粘贴,很容易出问题
2.3 leetcode 225:用队列实现栈
用一个队列实现栈没想出来,还是分了输入输出两个队列,但是这两个队列不是分别是输入输出关系了,因为队列是先进先出的规则,把一个队列中的数据导入另一个队列中,数据的顺序并没有变,并没有变成先进后出的顺序
所以另一个队列仅仅是记录最后一个元素之前的元素(与2.1 第一遍代码的思路一致),即另一个队列完全用来备份的
代码随想录对于这种思路的代码实现
class MyStack {
public:
queue<int> que1;
queue<int> que2; // 辅助队列,用来备份
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que1.size();
size--;
while (size--) { // 将que1 导入que2,但要留下最后一个元素
que2.push(que1.front());
que1.pop();
}
int result = que1.front(); // 留下的最后一个元素就是要返回的值
que1.pop();
que1 = que2; // 再将que2赋值给que1
while (!que2.empty()) { // 清空que2
que2.pop();
}
return result;
}
/** Get the top element. */
int top() {
return que1.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que1.empty();
}
};
上述的思路是固定 que2就是用来 备份的,如果轮流 使用两个队列中的一个来 作为备份队列,反正 跟用栈实现队列 不同的是:不管怎么在两个队列之间倒来倒去,反正元素在 队列内的顺序 是固定不变的。实现如下:
top()不能与pop()逻辑一样(因为pop最后留的一个元素是弹出的,所以造成每次转队列中的元素自然全部转出,肯定有一个栈是空的,而top()会留元素,可能出现两个元队列均有一个元素的情况),因为会两个队列转来转去,当pop先判断输入队列时,每次转队列中的元素必须全部转(即在输出剩余的一个元素时需要把那个元素也弄到另一个队列),不然在两个队列均有一个元素的时候会影响pop的size>1执行,会从输入队列(其实没有所谓的输入输出了)里把那个拉出来,其实应该拉输出队列的
即在top()获取剩余的那个元素数值后,加入逻辑:
stdOut.push(stdIn.front());
stdIn.pop();//队列中不能有残留的元素
以及
stdIn.push(stdOut.front());
stdOut.pop();//队列中不能有残留的元素
整体第一遍代码
class MyStack {
public:
queue<int> stdIn;
queue<int> stdOut;
MyStack() {
}
void push(int x) {
stdIn.push(x);
}
int pop() {
if(!stdIn.empty()) {
while(stdIn.size() > 1) {
stdOut.push(stdIn.front());
stdIn.pop();
}
int result = stdIn.front();
stdIn.pop();
return result;
}
else {
while(stdOut.size() > 1) {
stdIn.push(stdOut.front());
stdOut.pop();
}
int result = stdOut.front();
stdOut.pop();
return result;
}
}
int top() {
if(!stdIn.empty()) {
while(stdIn.size() > 1) {
stdOut.push(stdIn.front());
stdIn.pop();
}
int result = stdIn.front();
stdOut.push(stdIn.front());
stdIn.pop();//队列中不能有残留的元素
return result;
}
else {
while(stdOut.size() > 1) {
stdIn.push(stdOut.front());
stdOut.pop();
}
int result = stdOut.front();
stdIn.push(stdOut.front());
stdOut.pop();//队列中不能有残留的元素
return result;
}
//不能与pop()逻辑一样,因为会两个队列转来转去,每次转队列中的元素必须全部转,不然在两个队列均有一个元素的时候会影响pop的size>1执行
}
bool empty() {
return stdIn.empty() && stdOut.empty();
}
};
如果为了解决
“ 在两个队列均有一个元素的时候会影响pop的size>1执行,会从输入队列(其实没有所谓的输入输出了)里把那个拉出来,其实应该拉输出队列的 ”
单纯top函数中先判断输出队列
int top() {
if(!stdOut.empty()) {
while(stdOut.size() > 1) {
stdIn.push(stdOut.front());
stdOut.pop();
}
int result = stdOut.front();
return result;
}
else {
while(stdIn.size() > 1) {
stdOut.push(stdIn.front());
stdIn.pop();
}
int result = stdIn.front();
return result;
}
}
在两个队列均有一个元素的时候确实会从输出队列拉出来,正确了,但是如果在之前只有push(即输出队列没有元素)连着两个top,就会出现第二次top把第一次top留在输入队列的元素给输出出来了
优化
其实这道题目就是用一个队列就够了,其实不要两个队列倒来倒去,pop的时候将队列头部的元素(除了最后一个元素外)重新添加到队列尾部,此时再去弹出元素就是栈的顺序了(那留下的最后一个元素)为了确保知道往外弹多少个元素,前面设一个size进行次数的记录
class MyStack {
public:
queue<int> que;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que.size();
size--;
while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
que.push(que.front());
que.pop();
}
int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
que.pop();
return result;
}
/** Get the top element. */
int top() {
return que.back(); // 注意queue成员函数 不光可以输出 队首的,还可以 输出队尾的
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
};