zoukankan      html  css  js  c++  java
  • 寒假Day60:详解超简单的队列与栈

    队列

    • 特点:先进先出
    • 队列的图:

    • 队列的基本操作:

    #include<stdio.h>
    #include<queue>//队列的头文件
    using namespace std;//这个也一定要写
    
    //需求:利用queue把1、2、3、4、5都入队,然后依次取出2
    
    queue<int>Q;//设置一个队列叫做Q,存放int类元素
    
    int main()
    {
    
        while(!Q.empty())//清空队列内元素,根据需求
        {//如果开在函数里面或者多组数据时需要清空
            Q.front();
            Q.pop();
        }
        for(int i=1;i<=5;i++)
            Q.push(i);//依次入队
        bool flag=Q.empty();
        printf("判断栈此时是否为空:%d\n\n",flag);//输出1说明空,0说明存在元素
        int w=Q.front();//取出队首元素,此时是1
        printf("此时的栈顶元素:%d\n\n",w);
        Q.pop();//既然此时1已经被取出来了,那么就得从队列中把1删除,这个是删除操作
        w=Q.front();
        Q.pop();
        printf("需要取出的元素:%d\n\n",w);
        while(!Q.empty())
        {
            w=Q.front();
            printf("%d ",w);
            Q.pop();
        }//依次输出队列元素
        return 0;
    }

    优先队列

    其实和队列也差不多

    队列取出队首元素是front(),优先队列和栈都是top()

    例题1:

    HDU1873-看病要排队

    题目链接:https://vjudge.net/problem/HDU-1873

    类似于结构体,一个元素有多个属性,需要根据属性排个队

    但是你们可能就想到sort结构体排序了,但是每插入一个元素,sort都需要排一次,浪费大量时间,优先队列是每插入一个元素会自动排序

    struct node
    {
        int y,id;
        bool operator <(const node &a)const
        {
            if(a.y!=y)
                return a.y>y;
            return a.id<id;
        }
    };

    AC代码:

     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<queue>
     6 using namespace std;
     7 typedef long long ll;
     8 
     9 struct node
    10 {
    11     int y,id;
    12     bool operator <(const node &a)const
    13     {
    14         if(a.y!=y)
    15             return a.y>y;
    16         return a.id<id;
    17     }
    18 };
    19 
    20 char a[5];
    21 
    22 int main()
    23 {
    24     int n,aa,bb;
    25     priority_queue<node>Q[5];
    26     while(~scanf("%d",&n))
    27     {
    28         for(int i=1; i<=3; i++)
    29         {
    30             while(!Q[i].empty())
    31                 Q[i].pop();
    32         }
    33         int w=1;
    34         for(int i=1; i<=n; i++)
    35         {
    36             scanf("%s",a);
    37             if(a[0]=='I')
    38             {
    39                 cin>>aa>>bb;
    40                 node p;
    41                 p.id=w;
    42                 p.y=bb;
    43                 Q[aa].push(p);
    44                 w++;
    45             }
    46             else
    47             {
    48                 cin>>aa;
    49                 if(Q[aa].empty())
    50                     cout<<"EMPTY"<<endl;
    51                 else
    52                 {
    53                     node p;
    54                     // p=Q[aa].front();
    55                     p=Q[aa].top();
    56                     Q[aa].pop();
    57                     cout<<p.id<<endl;
    58                 }
    59             }
    60         }
    61     }
    62     return 0;
    63 }
    View Code

    例题2:

    如果还不了解哈夫曼树的话,这个可以先放一下

    POJ1521

    题目链接:http://poj.org/problem?id=1521

    参考博客:https://www.cnblogs.com/OFSHK/p/12544649.html

    记住这个就可以了:

    priority_queue<int,vector<int>,greater<int> >Q;

    这个表示从小到大入队。

    还有一个是:

    priority_queue<int, vector<int>, less<int> > Q;

    表示从大到小入队。

    区别一下,并且注意一下:注意最后两个>之间有一个空格,表示重载(这个以后了解就好啦)

    你们可能会想用sort,

    但是根据哈夫曼树来说,每次取出最小的两个元素,还需要将这两个元素相加的值再按照从小到大放入队列中,

    所以还是先去了解一个哈夫曼树这个数据结构吧。


    • 特点:先进后出
    • 栈的图:

    •  栈的基本操作:
    #include<stdio.h>
    #include<stack>//栈的头文件
    using namespace std;//这个也一定要写
    
    //需求:利用stack把1、2、3、4、5都入栈,但是需要利用出栈的形式取出3
    
    stack<int>s;//设置一个栈叫做s,存放int类元素
    
    int main()
    {
        while(!s.empty())//清空栈内元素,根据需求
        {//如果开在函数里面或者多组数据时需要清空
            s.top();
            s.pop();
        }
        s.push(1);//分辨让规定元素入栈
        s.push(2);
        s.push(3);
        int w=s.top();//取出栈顶元素,此时是3
        printf("此时的栈顶元素:%d\n\n",w);
        s.pop();//既然此时3已经被取出来了,那么就得从栈中把3删除,这个是删除操作
        s.push(4);//继续存入后继元素
        s.push(5);
        while(!s.empty())
        {
            w=s.top();
            printf("%d ",w);
            s.pop();
        }//依次输出栈内元素
        printf("\n");
        bool flag=s.empty();
        printf("判断栈此时是否为空:%d\n",flag);//输出1说明空
        return 0;
    }

    我感觉栈的用途还是比较少的,可能是我没碰到多少吧。

  • 相关阅读:
    C++实现单例模式
    进程间通信(IPC)介绍
    Python装饰器
    Python中import的使用
    Python中is和==的区别
    SK-Learn使用NMF(非负矩阵分解)和LDA(隐含狄利克雷分布)进行话题抽取
    Focal Loss for Dense Object Detection
    YOLOv3: An Incremental Improvement
    YOLO9000: Better, Faster, Stronger
    You Only Look Once: Unified, Real-Time Object Detection
  • 原文地址:https://www.cnblogs.com/OFSHK/p/12572390.html
Copyright © 2011-2022 走看看