0
点赞
收藏
分享

微信扫一扫

手写模拟队列

一:关于队列 

(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;
}
}
}

 



举报

相关推荐

队列与模拟队列

队列模拟(queue)

数组模拟队列

手写消息队列(基于RabbitMQ)

手写MQ,多线程模拟

你手写过堵塞队列吗?

使用数组模拟队列

0 条评论