STL:容器—栈

☀️Stack☀️

与队列不同,栈是一个LIFO数据结构。通常,插入操作在栈中被称作入栈push。与队列类似,总是在堆栈的末尾添加一个新元素。但是,删除操作,退栈pop,将始终删除队列中相对于它的最后一个元素。

Stack简介

stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口,形式如图所示。stack容器允许新增元素,移除元素,取得栈顶元素,但是除了最顶端外,没有任何其他方法可以存取stack的其他元素。换言之,stack不允许有遍历行为。
有元素推入栈的操作称为:push,将元素推出stack的操作称为pop.
在这里插入图片描述

⭕️Stack没有迭代器⭕️

和队列一样,可以看我的另一篇博客

常用方法

1、大小操作

size()  //大小
empty()  //判空

2、存取操作

push(elem)  //插入进栈顶
pop()  //弹出栈顶
top()  //访问栈顶

3、遍历

while(!s.empty())
{
	cout << s.top() << endl;
	s.pop();
}

4、综合练习

☁️是时候展示真正的力量了!奥利给!👍
💻这个代码必须在力扣上面运行,否则要加头文件

#include <iostream>

class MyStack {
    private:
        vector<int> data;               // store elements
    public:
        /** Insert an element into the stack. */
        void push(int x) {
            data.push_back(x);
        }
        /** Checks whether the queue is empty or not. */
        bool isEmpty() {
            return data.empty();
        }
        /** Get the top item from the queue. */
        int top() {
            return data.back();
        }
        /** Delete an element from the queue. Return true if the operation is successful. */
        bool pop() {
            if (isEmpty()) {
                return false;
            }
            data.pop_back();
            return true;
        }
};

int main() {
    MyStack s;
    s.push(1);
    s.push(2);
    s.push(3);
    for (int i = 0; i < 4; ++i) {
        if (!s.isEmpty()) {
            cout << s.top() << endl;
        }
        cout << (s.pop() ? "true" : "false") << endl;
    }
}

是不是对栈有了一定的了解啦?来做题吧!😄

Stack经典题目

📚1、判断回文-栈-超简单

同样不给代码,超简单,队栈都可实现

📚2、字符串翻转-超简单

不给代码,给点伪代码,自己思考
🎍用到string字符串类,也是STL
🎍用到char类型的stack
🎍遍历

#include <string>
#include <stack>

每次弹出赋值进入新的字符串,判断旧字符串和新字符串是否相等

📚3、删除最外层的括号-简单-力扣

题目地址:https://leetcode-cn.com/tag/stack/
在这里插入图片描述

class Solution {
public:
    string removeOuterParentheses(string S) {
        int L = 1; int R = 0; //左右指针
        string ans; //string就是STL模板库,可以实现模拟栈
        for(int i = 1; i < S.size(); i++){
            if(S[i] == '(')L++;
            else R++;
            if(R != L)
                ans.push_back(S[i]);
            else {
                i++;
                L = 1;
                R = 0;
            }
        }
        return ans;
    }
};

📚4、有效的括号-简单-经典-力扣

栈的经典应用就是配对括号
题目地址:https://leetcode-cn.com/problems/valid-parentheses/
简单!瞬间通过

class Solution {
public:
    bool isValid(string s) {
        if(s.length()%2!=0)return false;//如果长度为奇数,false
        stack<char> mystack;
        for(char &c : s){
            if(c=='(' || c=='[' || c=='{')mystack.push(c);//压入栈中
            else{
                if(mystack.empty())return false;
                if(c==')' && mystack.top()!='(')return false;
                if(c==']' && mystack.top()!='[')return false;
                if(c=='}' && mystack.top()!='{')return false;
                mystack.pop();
            }
        }
        return mystack.empty();
    }
};

5、更多题目

我们在LeetCode见👋

参考:

1、https://blog.csdn.net/qq_42322103/article/details/99685797
2、https://leetcode-cn.com/tag/stack/
3、https://leetcode-cn.com/explore/learn/card/queue-stack/218/stack-last-in-first-out-data-structure/875/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章