您的位置:首页 > 产品设计 > UI/UE

acm中Stack和Queue使用demo

2015-12-04 20:38 477 查看
栈(Stack)

后进先出(LIFO)

#include<stack> //添加头文件
#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
stack<int>s; //声明int类型数据的栈 
s.push(1); //往栈里添加元素
s.push(2);
s.push(3);
printf("%d\n",s.top());//获取栈顶元素
s.pop(); //弹出栈顶元素
printf("%d\n",s.top());
s.pop();
printf("%d\n",s.top());
s.pop();
return 0;
}

队列(Queue)

先进先出(FIFO)

#include<queue> //添加头文件
#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
queue<int>q; //声明int类型数据的队列
q.push(1); //往栈里添加元素
q.push(2);
q.push(3);
printf("%d\n",q.front());//获取队头元素
q.pop(); //移除队尾元素
printf("%d\n",q.front());
q.pop();
printf("%d\n",q.front());
q.pop();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: