特殊的容器:容器适配器
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.02 NOIP模拟 距离(斜率优化dp)

    传送门 分四个方向分别讨论. 每次枚举当前行iii,然后对于第二维jjj用斜率优化dpdpdp. f[i][j]=(j−k)2+mindisk2f[i][j]=(j-k)^2+mindis_k^2f[ ...

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

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

  3. lnmp源码编译安装zabbix

    软件安装 Mysql 安装 tar xf mysql-5.7.13-1.el6.x86_64.rpm-bundle.tar -C mysql rpm -e --nodeps  mysql-libs-5 ...

  4. 第25章:MongoDB-文档存储[理解]

    ① 将文档插入到MongoDB的时候,文档是按照插入的顺序,依次在磁盘上相邻保存 因此,一个文档变大了,原来的位置要是放不下这个文档了,就需要把这个文档移动到集合的另外一个位置,通常是最后,能放下这个 ...

  5. javascript 连等赋值问题

    var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);// - ...

  6. 批处理最完整人性化教程(.bat文件语法)

    原文链接:http://www.cnitblog.com/seeyeah/archive/2009/01/15/53808.html 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,你要你识字 ...

  7. POJ3111 K Best 2017-05-11 18:12 31人阅读 评论(0) 收藏

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10261   Accepted: 2644 Case Time ...

  8. Foundation-NSRunLoop

    Objective-C之run loop详解 Objective-C之run loop详解 RunLoop 详解

  9. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  10. kmp小记

    以下转载自Matrix67 ************************************************************************************** ...