队列和栈

栈的基本操作
1.pop 操作 弹出顶元素
2.top 或则 peek 操作 查看顶元素
3.push 操作 进栈操作
4.size 操作

队列基本操作(一端进,一端出)
1.push 操作在队尾加入
2.pop 操作从队头弹出

图的遍历
深度优先遍历(DFS) 栈实现
宽度优先遍历 (BDF) 队列实现

最值栈:定义栈的数据结构,实现一个能够得到栈最小元素的Min 函数。

class Solution{
public:
    Stack<int> source_data,min_data;
    void push(int value){
        source_data.push(value);
        if(min_data.empt()||min_data.top()>=value)
            min_data.push(value);
        else
            min_data.push(min_data.top());
    }
    void pop(){
        source_data.pop();
        min_data.pop();
    }
    int top(){
        return source_data.top();
    }
    int min(){
        return min_data.top();
    }
}

双栈队列:只用两个栈实现队列
思路:注意弹出时候,需要判断弹出栈是否为空。输入栈转到弹出栈时候,需要一次性全部压入。

class TwoStack {
public:
    stack<int> stack_push,stack_pop;
    vector<int> twoStack(vector<int> ope, int n) 
    {
        vector<int> res;
        int i;
        for(i=0;i<n;i++)
        {
            if(ope[i]>0)
                push(ope[i]);
            if(ope[i]==0)
                res.push_back(pop());
            if(ope[i]<0)
                exit(-1);
        }
        return res;
    }
    void push(int value)
    {
        stack_push.push(value);
    }
    int pop()
    {
        if(stack_pop.empty())
            while(!stack_push.empty())
            {
                stack_pop.push(stack_push.top());
                stack_push.pop();
            }
            int res=stack_pop.top();
            stack_pop.pop();
            return res;
    }

};

栈的反转:实现一个栈的逆序,只能用递归函数和栈本身的pop操作,不能申请额外的数据结构。

class StackReverse{
public :
    vector<int> reverseStack(vector<int> A,int n){
        stack<int> sta;
        int i;
        for(i = n-1;i>=0;i--)
            sta.push(A[i]);
        revStack(sta);
        vector<int> res;
        while(!sta.empty()){
            res.push_back(sta.top());
            sta.pop();
        }
        return res;
    }
    void revStack(stack<int> &A){
    //每次递归获取栈底元素,并删除了栈底元素,然后压入操作。
        if(A.empty())
            return :
        itn res= Get(A);
        revStack(A);
        A.push(res);
    }
    int Get(Stack<int> &A){
    //移除栈底元素,并返回  传递一个不变的值,但是每一次传递时候都进行其他操作
        if(A.empty())
            exit(-1);
        int res1 = A.top();
        A.pop();
        if(A.empty())
            return res1;
        else{
            int res2 = Get(A);
            A.push(res1);
            return res2;
        }
    }
};

排序栈:按升序对栈进行排序,最多智能使用一个额外的栈存放临时数据。

class TwoStacks {
public:
    vector<int> twoStacksSort(vector<int> numbers){
        stack<int> sta;
        int i;
        for(i = n-1;i>=0;i--)
            sta.push(A[i]);
        StackSort(sta);
        ...
    }
    void StackSort(stack<int> &sta){
        stack<int> help;
        while(!sta.empty()){
            int res = sta.top();
            sta.pop();
            if(help.empty()||res<help.top())
                help.push(res);
            else{
                while(!help.empty()&& res>help.top()){
                    sta.push(help.top());
                    help.pop();
                }
                help.push(res);
            }
        }
        while(!help.empty()){
            sta.push(help.pop());
        }

    }
;

猜你喜欢

转载自blog.csdn.net/jcsyl_mshot/article/details/80587703
今日推荐