队列:
- 基本的数据结构之一,特点是“先进先出”。
- 例如排队,先进队列的,先得到服务。

队列的有关操作:
|
例子 |
说明 |
|
queue <Type> q; |
定义栈,Type为数据类型,如int,float,char等 |
|
q. push(item); |
把item放进队列 |
|
q.front(); |
返回队首元素,但不会删除 |
|
q.pop(); |
删除队首元素 |
|
q.back(); |
返回队尾元素 |
|
q.size(); |
返回元素个数 |
|
q.empty(); |
检查队列是否为空 |
例题:
- hdu 1702 ACboy needs your help again!
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,temp;
cin>>t;
while(t--)
{
string str,str1;
queue<int>Q;
stack<int>S;
cin>>n>>str;
for(int i=0; i<n; i++)
{
if(str=="FIFO") //队列
{
cin>>str1;
if(str1=="IN")
{
cin>>temp;
Q.push(temp);
}
if(str1=="OUT")
{
if(Q.empty()) cout<<"None"<<endl;
else
{
cout<<Q.front()<<endl;
Q.pop();
}
}
}
else //栈
{
cin>>str1;
if(str1=="IN")
{
cin>>temp;
S.push(temp);
}
if(str1=="OUT")
{
if(S.empty()) cout<<"None"<<endl;
else
{
cout<<S.top()<<endl;
S.pop();
}
}
}
}
}
return 0;
}


博客介绍了队列这一基本数据结构,其特点是“先进先出”,并以排队为例进行说明。还提及了队列的有关操作,给出了hdu 1702 “ACboy needs your help again!”这一例题。

被折叠的 条评论
为什么被折叠?



