特殊的容器:容器适配器
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.10.31 NOIP训练 锻造(方程式期望入门题)(期望dp)

    传送门 根据题目列出方程: fi=pi∗(fi−1+fi−2)+(1−pi)∗(fi+1+fi)f_i=p_i*(f_{i-1}+f_{i-2})+(1-p_i)*(f_{i+1}+f_i)fi​=p ...

  2. java常量

    一.使用常量的好处 1.便于维护

  3. 使用hibernate从一方获取多方信息时报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

    引起原因:hibernate加载关联对象的方式有懒加载方式和立即加载方式. 如果在多对一的配置中没有指定加载方式,而一对多的配置中指定了懒加载方式,因此在获取一方是可获取到值,而获取多方时sessio ...

  4. c++关键字static的作用

    1.隐藏 当同时编译多个源文件(.cpp)时,在某一个源文件中用static修饰的全局变量或函数,对其他源文件是隐藏的 //1.cpp ; void fun() { cout << < ...

  5. Keras人工神经网络多分类(SGD)

    import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import ...

  6. (转)ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器

    上传文件有两个主要的目的地,一个是服务器,另一个是数据库,ASP.NET内置了FileUpload这个上传控件,文本框显示用户选择的文件的全名. 其属性主要包括: ContenLength:上传文件大 ...

  7. Path类对路径字符串的操作

    在写程序时,不时会用到处理文件路径的问题,例如:取得扩展名.从路径中取出文件名.路径合并.取出或者去年扩展名等.这些功能都可以通过System.IO.Path类提供的方法来实现.这些相关功能用过多次了 ...

  8. VC6.0编译器设置

    主要通过VC的菜单项Project->Settings->C/C++页来完成.我们可以看到这一页的最下面Project Options中的内容,一般如下:/nologo /MDd /W3 ...

  9. (数学)Knight's Trip -- hdu -- 3766

    http://acm.hdu.edu.cn/showproblem.php?pid=3766 Knight's Trip Time Limit: 2000/1000 MS (Java/Others)  ...

  10. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...