栈与队列理论基础
文档讲解 : 代码随想录 - 栈与队列理论基础
状态:再次回顾。
栈:先进后出
队列:先进先出
STK(C++标准库)
- HP STL 其他版本的C++ STL,一般是以HP STL为蓝本实现出来的,HP STL是C++ STL的第一个实现版本,而且开放源代码。
- P.J.Plauger STL 由P.J.Plauger参照HP STL实现出来的,被Visual C++编译器所采用,不是开源的。
- SGI STL 由Silicon Graphics Computer Systems公司参照HP STL实现,被Linux的C++编译器GCC所采用,SGI STL是开源软件,源码可读性甚高。
1. C++中stack是容器么?
栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)。
所以STL中栈往往不被归类为容器,而被归类为container adapter (容器适配器)。
2. 我们使用的stack是属于哪个版本的STL?
栈和队列是SGI STL里面的数据结构
3. 我们使用的STL中stack是如何实现的?
栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。
我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。
SGI STL中队列一样是以deque为缺省情况下的底部结构。
4. stack 提供迭代器来遍历stack空间么?
栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。 不像是set 或者map 提供迭代器iterator来遍历所有元素。
队列中先进先出的数据结构,同样不允许有遍历行为,不提供迭代器
232.用栈实现队列
文档讲解 : 代码随想录 - 232.用栈实现队列
状态:再次回顾。
本题思路图解:
本题代码 (ACM)
#include <iostream>
#include <stack>
using namespace std;
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();
}
};
int main() {
MyQueue myQueue = MyQueue();
myQueue.push(1); // queue is : [1]
myQueue.push(2); // queue is : [1, 2] (leftmost is front of the queue)
cout << myQueue.peek() << endl; // return 1
cout << myQueue.pop() << endl; // return 1, queue is [2]
if (myQueue.empty()) {// return false
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
return 0;
}
- 时间复杂度: push和empty为O(1), pop和peek为O(n)
- 空间复杂度: O(n)
225. 用队列实现栈
文档讲解 : 代码随想录 - 225. 用队列实现栈
状态:再次回顾。
本题思路图解:
本题代码 (ACM)
#include <iostream>
#include <queue>
using namespace std;
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();
}
};
int main() {
MyStack myStack = MyStack();
myStack.push(1);
myStack.push(2);
cout << myStack.top() << endl; // 返回 2
cout << myStack.pop() << endl; // 返回 2
if (myStack.empty()) { // 返回 false
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
return 0;
}
- 时间复杂度: push为O(n),其他为O(1)
- 空间复杂度: O(n)