stack与queue
2014-08-11 19:38
120 查看
stack
stack模板类的定义在<stack>的头文件中;
定义stack对象的范例如下;
stack<int> s;
stack<double> s;
stack的基本操作:
s.top() ; //栈顶
s.push(a) ; //元素a入栈
s.pop(); //栈顶元素出栈
s.empty(); //判断栈空,如果为空,则返回true
s.size(); //返回栈中元素个数;
代码示例:
queue
queue模板类定义在<queue>的头文件中;
定义queue对象的范例如下:
queue<int> q;
queue<double> q;
queue的基本操作:
q.front(); //队首
q.back(); //队尾
q.push(a); //元素a入队
q.pop(); //出队
q.empty(); //判断队列空,如果为空,返回true
q.size(); //返回队中元素个数
stack模板类的定义在<stack>的头文件中;
定义stack对象的范例如下;
stack<int> s;
stack<double> s;
stack的基本操作:
s.top() ; //栈顶
s.push(a) ; //元素a入栈
s.pop(); //栈顶元素出栈
s.empty(); //判断栈空,如果为空,则返回true
s.size(); //返回栈中元素个数;
代码示例:
#include <iostream> #include <stack> using namespace std; stack<int> s; int main(){ int n ; cin>>n; for(int i = 0 ; i < n ; i++) s.push(i+1); cout<<s.size()<<endl; while(!s.empty()){ cout<<s.top()<<"\t"; s.pop(); } cout<<endl; system("pause"); return 0; }
queue
queue模板类定义在<queue>的头文件中;
定义queue对象的范例如下:
queue<int> q;
queue<double> q;
queue的基本操作:
q.front(); //队首
q.back(); //队尾
q.push(a); //元素a入队
q.pop(); //出队
q.empty(); //判断队列空,如果为空,返回true
q.size(); //返回队中元素个数
#include <iostream> #include <queue> using namespace std; queue<int> q; int main(){ int n ; cin>>n; for(int i = 0 ; i < n ; i++) q.push(i+1); cout<<q.size()<<endl; while(!q.empty()){ cout<<q.front()<<"\t"; q.pop(); } cout<<endl; system("pause"); return 0; }
相关文章推荐
- C#集合类(Array、Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- C++ young 程序库——y_queue.hpp 和 y_stack.hpp
- LeetCode Summary STL Stack Queue
- erl_stack_queue-队列求迷宫最短路径
- C++ STL基本容器的使用(vector、list、deque、map、stack、queue)
- C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue
- C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue
- stack,deque,queue对比
- 深入理解容器系列片之三--------LinkedList、Stack、Queue、PriorityQueue的总结
- List Queue Map Stack的遍历
- 1282: Stack ? Queue ?
- Stack和Queue:后进先出和先进先出
- stack实现queue ; list实现stack
- 用两个栈(Stack)实现一个队列(Queue)
- C# 集合类 Array,Arraylist,List,Hashtable,Dictionary,Stack,Queue学习资料(非常重要)
- 链表实现stack和queue,java实现(ADT思想)
- C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue
- C++ STL--stack/queue 的使用方法
- 学用 ASP.Net 之 System.Collections.Queue 与 Stack 类
- 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战