手写模拟队列

一:关于队列 

(1)一种线性表

(2)允许在表的一端插入数据,在另一端删除元素。插入元素的这一端称之为队尾。删除元素的这一端为队首

(3)先进先出,就像排队一样

二:操作:

定义q[],hh队头,tt队尾

(1)插入

q[++tt]=x

(2)弹出队头

hh++

(3)判断空

if(hh<=tt)为空

else 不为空

(4)取出队头

q[hh]

三:基本板子题

#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=1e5+10,maxn2=31*maxn;
int q[maxn];//ÏȽøÏȳö 
int hh,tt;//¶ÓÍ·£¬Î² 
int main()
{
    int m;
    cin>>m;
    hh=1;
    tt=0;
    while(m--)
    {
        char op[11];
        cin>>op;
        if(!strcmp(op,"push"))
        {
            int x;
            cin>>x;
            q[++tt]=x;
        }
        else if(!strcmp(op,"pop"))
        {
            hh++;
        }
        else if(!strcmp(op,"empty"))
        {
            if(hh<=tt)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
        else if(!strcmp(op,"query"))
        {
            cout<<q[hh]<<endl;            
        }
    }
}

 

posted @ 2020-11-07 21:07  liyexin  阅读(159)  评论(0编辑  收藏  举报