Queue and Stack
2015-03-15 15:32
441 查看
Basic operations for queue and stack.
#include<iostream>
#include<list>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
template <class T, class Container>
void printStack(stack<T, Container> s){
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
}
template <class T, class Container>
void printQueue(queue<T, Container> s){
while(!s.empty()){
cout << s.front() << endl;
s.pop();
}
}
int main(){
stack<int, vector<int> > s;
for(int i=0; i<4; i++){
s.push(i+1);
}
printStack(s);
string str = "a";
queue<string, deque<string> > t;
for(int i=0; i<4; i++){
t.push(str);
str += 'a';
}
printQueue(t);
return 0;
}
We could also extend basic stack class, for instance, create a stack class with limitation of its size.
#include<iostream>
#include<list>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
template<class T, class Container=deque<T> >
class mystack : public stack<T, Container>{
private:
int MaxSize;
public:
mystack():MaxSize(0){}
mystack(int MaxSize_):MaxSize(MaxSize_){}
void push(const T& t){
if(int(stack<T, Container>::size())<MaxSize){
stack<T, Container>::push(t);
}else{
cout << "stack is fill, cannot add items!" << endl;
}
}
};
int main(){
mystack<int, deque<int> > s(2);
cout << s.size() << endl;
s.push(1);
s.push(2);
s.push(3);
return 0;
}Here, we call the function in base class via
stack<T, Container>::push(t)
#include<iostream>
#include<list>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
template <class T, class Container>
void printStack(stack<T, Container> s){
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
}
template <class T, class Container>
void printQueue(queue<T, Container> s){
while(!s.empty()){
cout << s.front() << endl;
s.pop();
}
}
int main(){
stack<int, vector<int> > s;
for(int i=0; i<4; i++){
s.push(i+1);
}
printStack(s);
string str = "a";
queue<string, deque<string> > t;
for(int i=0; i<4; i++){
t.push(str);
str += 'a';
}
printQueue(t);
return 0;
}
We could also extend basic stack class, for instance, create a stack class with limitation of its size.
#include<iostream>
#include<list>
#include<vector>
#include<string>
#include<stack>
#include<queue>
using namespace std;
template<class T, class Container=deque<T> >
class mystack : public stack<T, Container>{
private:
int MaxSize;
public:
mystack():MaxSize(0){}
mystack(int MaxSize_):MaxSize(MaxSize_){}
void push(const T& t){
if(int(stack<T, Container>::size())<MaxSize){
stack<T, Container>::push(t);
}else{
cout << "stack is fill, cannot add items!" << endl;
}
}
};
int main(){
mystack<int, deque<int> > s(2);
cout << s.size() << endl;
s.push(1);
s.push(2);
s.push(3);
return 0;
}Here, we call the function in base class via
stack<T, Container>::push(t)
相关文章推荐
- Queue and Stack
- stack and queue
- Selection Sort with stack and queue
- Queue and stack
- Data structures for game programming learning notes (六)——stack and queue
- Stack and Queue 栈和队列
- Adaptive Container: stack and queue
- List, Queue and Stack
- stack-structured algorithm and queue-structured algorithm
- Insertion Sort with queue and stack
- Stack and queue.
- apply stack and Queue symbol
- TStack and TQueue
- queue by one stack、two stack and three stack
- Stack and Queue
- Stack and Queue
- 栈与队列(queue and stack)
- simple version of stack and queue 's ADT
- C++ STL--stack/queue 的使用方法
- java中List、Map、Set、Collection、Stack、Queue等的使用