特殊的容器:容器适配器
stack     queue     priority_queue:vector+堆算法---->优先级队列
stack:
    1.栈的概念:特殊的线性结构,只允许在其一端进行插入删除操作---栈顶,另一端称为栈底
    2.栈的特性:后进先出 LIFO 
    3.栈的作用:
    4.栈的应用:
    #include<iostream>
    using namespace std;
    class Date
    {
        public:
        Date(int year=1900,int month=11,int day=10){
            _year=year;
            _month=month;
            _day=day;
        }
        Date(const Date& d){
            _year=d._year;
            _month=d._month;
            _day=d._day;
        }
        private:
        int _year;
        int _month;
        int _day;
    }
    int main(){
    Date d;
    stack<Date> s;
    s.push(d);//参数为类类型对象的引用
    s.empace(2018,11,10);//效率比较高,不会调用拷贝构造
    return 0;
    }
    栈中没有迭代器,不需要,因为栈的操作已经给死了
最小栈:必须满足栈的特性
    push/pop/top--->O(1)
    min--->O(1)
    实现最小栈的方法:使用两个栈A、B(一个放最小值,一个放数据),一个栈也行
struct Elem{
     int data;
    int min;
}
stack<int> _data;
stack<int> _min;
int top(){
  return data.top();
}
int getmin(){
 return _min.top();
}
void Pop(){
    
}
void Push(int x){
 _data.push(x);
 if(_min.empty()||_min.top()>=x){
    _min.push(x);
    }
}
    逆波兰表达式:
class Solution{
public: 
    int evalRPN(vector<string>& tokens){
        stack<int> s;
        for(size_t i=0;i<tokens.size();++i){
            string& str =tokens[i];
            if(!("+"==str||"-"==str||"*"==str||"/"==str)){
                s.push(atoi(str.c_str()));
            else{
                int right=s.top(),s.pop();
                int left=s.top(),s.pop();
                switch(str[0]){
                case '+':
                    s.push(left+right);
                    break;
                case '-':
                    s.push(left-right);
                    break;
                case '*':
                         s.push(left*right);
                    break;
                case '/':
                    s.push(left/right);
                    break;
                }
            }
        }
    return s.top();
}
 
二叉树的公共祖先结点:
bool GetNoePath(TreeNode * root,TreeNode *p,Stack<TreeNode*>& Path){
    if(NULL==root||NULL==p){
        return false;
    }
    path.push(root);
    if(root==p)
        rturn true;
    if(GetNodePath(root->left,p,path)){
        return true;
    if(GetNodePath(root->rigth,p,path)){
        retrn true;
    path.pop();
    return false;
}
 
TreeNode* lowestCommonAncestor(TreeNode* root,TreeNode* p,TreeNode *q){
    if(NULL==root||NULL==p||NULL==q){
        return NULL;
    }
    stack<TreeNode*> s1;
    stack<TreeNode*> s2;
    GetNodePath(root,p,s1);
    GetNodePath(root,q,s2);
    int size1=s1.size();
    int size2=s2.size();
    while(size1&&size2){
        if(size1<size2){
            s1.pop();
            size1--;
        }
        else if(size2<size1){
            s2.pop();
            size2--;
            }
        else{
            if(s1.top()==s2.top()){
                return s1.top();
            else{
                s1.pop();
                s2.pop();
                size1--;
                size2--;
                }
        }
    return NULL;
}
 
bool IsNodeInTree(root,Node){
    if(NULL==root||node==NULL){
        return false;
    }
    if(root==Node){
        return true;
    }
    bool Ret=false;
    if(Ret==IsNodeInTree(root->left,Node){
        return true;
    return IsNodeInTree(root->right,Node);
}
TreeNode* lowestCommonAncestor(TreeNode* root,TreeNode* p,TreeNode *q){
    if(NULL==root||NULL==p||NULL==q){
        return NULL;
    if(p==root||q==root){
        return root;
    bool isleft1=IsNodeInTree(root->left,p);
    bool isright2=false;
    if(isleft1){
        isright2=IsNodeInTree(root->right,q);
        if(isright2){
            return root;
        }
    }
    bool islift2=IsNodeTree(root->left,q)
    if(islift2)
        bool isright2=IsNodeTree(root->right,p);
        if(isright2){
            return root;
        }
    }
    if(isleft1&&isleft2){
        return lowestCommonAncestor(root->left,p,q);
    return lowestCommonAncestor(root->right,p,q);
}
        
 
 
二叉树的层序遍历:
层序遍历:按照从上往下,每一层从左往右依次遍历
   根据队列的数据结构进行操作
   1.将根节点放入到队列中
   循环:循环结束条件(队列不为空(!EMptyQueue(&q)))
      从队列中取队头元素
      访问该元素
      如果该元素的左子树存在,入队列
      如果该元素的右子树存在,入队列
      将对头元素出队列
vector<vector<int>> leve10Order(TreeNode* root){
    vector<vector<int>> vRet;
    if(NULL==root){
        return vRet;
    }
    Queue<TreeNode*> q;
    q.push(root);
    while(!q.empty()){
        size_t leve1count=q.size();
        vector<int> levelData;
        for(size_t i=0;i<levecount;++i){
            TreeNode* pCur=q.front();
            levelData.push_back(pCur->val);
            q.pop();
            if(pCur->left){
                q.push(pCur->left);
            }
            if(pCur->right){
                q.push(pCur->right);
            }
        vRet.push_back(levelData);
    }
    return vRet;
}
 
 
priority_queue:
    template<class T,class Con= vector<T>,class Compare=less<T>>
less   greater
    priority_queue<int ,vector<int>,greater<int>> 
    greater默认按照大于号方式给,创建小堆(此时vector<int(类型)一定要给,否则报错)
    默认:less  小于方式比  ---->  创建了大堆
 
topk问题:
    sort(array,array+6);给数组排序,
    sort(array,array+6,greater<int>)
 
 
 

Stack&&Queue的更多相关文章

  1. STL容器适配器 stack, queue

    stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...

  2. STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

      list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [uno ...

  3. 数据结构设计 Stack Queue

    之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...

  4. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  5. js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process

    js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process ...

  6. Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector

    Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同元素.在列表上迭代通常优于索引遍历.特殊的ListIterator迭代器允许元素插入.替换,双向访问, ...

  7. 第11天 Stack Queue

    1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...

  8. 特殊集合 Stack Queue Hashtable

    //Stack    干草堆集合    栈集合      先进后出 Stack st = new Stack(); //实例化 初始化 st.Push(2); //添加元素 st.Push(6); s ...

  9. java三篇博客转载 详解-vector,stack,queue,deque

    博客一:转载自http://shmilyaw-hotmail-com.iteye.com/blog/1825171 java stack的详细实现分析 简介 我们最常用的数据结构之一大概就是stack ...

随机推荐

  1. 2018.11.01 loj#2319. 「NOIP2017」列队(线段树)

    传送门 唉突然回忆起去年去noipnoipnoip提高组试水然后省二滚粗的悲惨经历... 往事不堪回首. 所以说考场上真的有debuffdebuffdebuff啊!!!虽然当时我也不会权值线段树 这道 ...

  2. 定时调度系列之Quartz.Net详解(转)

    出处:https://www.cnblogs.com/yaopengfei/p/9216229.html 一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提 ...

  3. 第1章 Python数据模型

    #<流畅的Python>读书笔记 # 第一部分 序幕 # 第1章 Python数据模型 # 魔术方法(magic method)是特殊方法的昵称.于是乎,特殊方法也叫双下方法(dunder ...

  4. myBatis中if test 字符串注意事项

    错误写法: <if test="userName == 'boshen'"> AND `USER_NAME` = #{userName} </if> 正确写 ...

  5. string流

    istringstream和ostringstream 从istringstream类中读取数据赋值给某个string,写入某个string到ostringstream类,头文件<sstream ...

  6. 根据方法名获取方法Body Content

    利用 MethodBody类的GetILAsByteArray方法可以获取到返回字节数组的MSIL的body.然后再去解析此字节数组, 可以得到MSIL,然后你再去解析MSIL,你就可以得到你想到so ...

  7. asp.net微信支付发起页面jsapi.aspx

    jsapi.aspx 后台核心代码 //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(Context); //初始化 p ...

  8. security.php

    <?php /** * */ class Security { public function csrf_verify() { if(count($_POST) == 0) { return ' ...

  9. poj 2676 如何填满九宫格

    Sudoku Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class ...

  10. 微擎开启redis memcache

    微擎开启redis memcache 2018年01月20日 14:39:54 luogan129 阅读数:2161更多 个人分类: 微信开发   版权声明:本文为博主原创文章,未经博主允许不得转载. ...