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;
}
后进先出(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;
}
相关文章推荐
- java中String、StringBuffer、StringBuilder的区别
- UITableVIew 滚动流畅性优化
- 37.拖放控件Interface Builder
- MySql提示:The server quit without updating PID file(…)失败
- UiAutomator使用步骤
- (2)轻松构建DuiLib HelloWorld By DuiCreator(下)
- (1)轻松构建DuiLib HelloWorld By DuiCreator(上)
- HDU 5057 Argestes and Sequence 分块
- 关于UI适配的文档
- UITextView 光标定位
- ugui和ngui手写虚拟摇杆功能比较
- UI - UISearchBar
- LeetCode 187 Repeated DNA Sequences
- hdu2604 Queuing
- 关于使用UIAlertView之后pop或者push时键盘闪退问题解决方法
- MUI移动开发框架——微信支付(android部分)
- android-UI组件实例大全(十五)------其他对话框:ProgressDialog,Date/TimePickerDialog
- android-UI组件实例大全(十四)------对话框的父类:AlertDialog
- SoapUI———mock数据工具
- android-UI组件实例大全(十三)------消息提示框:Toast和Notification