栈和队列的底层实现
参考:代码随想录
栈和队列是SGI STL里面的数据结构。
SGI STL 由Silicon Graphics Computer Systems公司参照HP STL实现,被Linux的C++编译器GCC所采用,SGI STL是开源软件,源码可读性甚高。
栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。 不像是set 或者map 提供迭代器iterator来遍历所有元素。
栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)。
所以STL中栈往往不被归类为容器,而被归类为container adapter(容器适配器)。
我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的低层结构。
deque是一个双向队列,只要封住一段,只开通另一端就可以实现栈的逻辑了。
我们也可以指定vector为栈的底层实现,初始化语句如下:
std::stack<int, std::vector<int> > third; // 使用vector为底层容器的栈
SGI STL中 队列底层实现缺省情况下一样使用deque实现的。
队列中先进先出的数据结构,同样不允许有遍历行为,不提供迭代器, SGI STL中队列一样是以deque为缺省情况下的底部结构。
所以STL 队列也不被归类为容器,而被归类为container adapter( 容器适配器)。
也可以指定list 为起底层实现,初始化queue的语句如下:
std::queue<int, std::list<int>> third; // 定义以list为底层容器的队列
232.用栈实现队列
题目:232. 用栈实现队列
class MyQueue {
public:
//两个栈
stack<int> qIn;
stack<int> qOut;
MyQueue() {
}
void push(int x) {
qIn.push(x);
}
int pop() {
if(qOut.empty()){
while(!qIn.empty()){
qOut.push(qIn.top());
qIn.pop();
}
}
//if(qOut.empty()) return null;
int res = qOut.top();
qOut.pop();
return res;
}
int peek() {
// if(qOut.empty()){
// while(!qIn.empty()){
// qOut.push(qIn.top());
// qIn.pop();
// }
// }
//学会复用写过的代码
int res = this->pop();
qOut.push(res);
return res;
}
bool empty() {
// if(qIn.empty() && qOut.empty()) return true;
// return false;
return qIn.empty() && qOut.empty()
}
};
225. 用队列实现栈
题目:225. 用队列实现栈
class MyStack {
public:
queue<int> q;
MyStack() {
}
void push(int x) {
q.push(x);
}
int pop() {
int n = q.size();
while(n != 1){
int x = q.front();
q.pop();
q.push(x);
n--;
}
int y = q.front();
q.pop();
return y;
}
int top() {
// int y = this->pop();
// q.push(y);
int y = q.back();
return y;
}
bool empty() {
return q.empty();
}
};
20. 有效的括号
题目:20. 有效的括号
class Solution {
public:
bool isValid(string s) {
stack<char> sta;
for(char c : s){
if(c == '(' || c == '{' || c == '['){
sta.push(c);
}
else{
if(sta.empty()) return false;
char top = sta.top();
if( (top == '(' && c == ')' ) ||
(top == '[' && c == ']' ) ||
(top == '{' && c == '}' )
){
sta.pop();
}else{
return false;
}
}
}
return sta.empty();
}
};
//优化代码
class Solution {
public:
bool isValid(string s) {
if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求
stack<char> st;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') st.push(')');
else if (s[i] == '{') st.push('}');
else if (s[i] == '[') st.push(']');
// 第三种情况:遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
// 第二种情况:遍历字符串匹配的过程中,发现栈里没有我们要匹配的字符。所以return false
else if (st.empty() || st.top() != s[i]) return false;
else st.pop(); // st.top() 与 s[i]相等,栈弹出元素
}
// 第一种情况:此时我们已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false,否则就return true
return st.empty();
}
};
1047. 删除字符串中的所有相邻重复项
class Solution {
public:
string removeDuplicates(string s) {
stack<char> sta;
for(char c : s){
if(sta.empty() || sta.top() != c) sta.push(c);
else sta.pop();
}
string ans = "";
while(!sta.empty()){
ans += sta.top();
sta.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
};
总结
了解队列和栈的底层原理都是容器适配器,了解栈的用法,如括号匹配,爱消除。