template<typename Y>用两个queue实现stack
来源:互联网 发布:伦敦金数据网 编辑:程序博客网 时间:2024/06/15 14:56
题目名称 : [Template]Review Template and Stack (eden)
时间限制 : 1000 ms
空间限制 : 32 MB
Description
**key points: **
template class and function, stack and queue.
Description:
In this assignment, you need to complete Class Stack's declaration and definition with Template. The different thing is that the Stack is implemented by two queues. Following is the example of Stack in integer:
class Stack { public: Stack(); // constructor. void push(const int& data); // push operation. int pop(); // return the value in the top and pop it out of the stack. int top(); // return the value in top. int size() const; // return size of the stack. bool empty(); // check whether is empty. private: queue<int> q1; // two queues. queue<int> q2; int count; // the number of elements.};
And you need to define a print function to print the content in the Stack from top to bottom. The format is: every element is followed by a blank space and an endl in the end.
Extra:
You are not allowed to use any STL except "queue".
For more detail, see the codes in main.cpp.
Stack.h:
#include <queue>
#include <iostream>
using namespace std;
template <typename T>
class Stack
{
public:
Stack();
void push(const T& data);
T pop();
T top();
int size() const;
bool empty();
private:
queue<T> q1;
queue<T> q2;
int count;
};
template<typename T>
Stack<T>::Stack()
{
count = 0;
}
template<typename T>
int Stack<T>::size() const
{
return count;
}
template<typename T>
bool Stack<T>::empty()
{
return count == 0;
}
template<typename T>
void Stack<T>::push(const T& data)
{
if((q1.empty() && q2.empty()) || (!q1.empty()))
{
q1.push(data);
}
else if(!q2.empty())
{
q2.push(data);
}
count++;
}
template<typename T>
T Stack<T>::pop()
{
if(!empty())
{
if(q2.empty())
{
T value = q1.back();
int size = q1.size() - 1;
while(size--)
{
q2.push(q1.front());
q1.pop();
}
q1.pop();
count--;
return value;
}
else if(q1.empty())
{
T value = q2.back();
int size = q2.size() - 1;
while(size--)
{
q1.push(q2.front());
q2.pop();
}
q2.pop();
count--;
return value;
}
}
}
template<typename T>
T Stack<T>::top()
{
if(!q1.empty())
{
return q1.back();
}
else return q2.back();
}
template<typename T>
void print(Stack<T> stack)
{
while(!stack.empty())
{
cout<<stack.top()<<" ";
stack.pop();
}
cout<<endl;
}
main.cpp:
#include "Stack.h"
#include<iostream>
#include<exception>
using namespace std;
class StackForbidden : public exception {
virtual const char *what() const throw() {
return "Please do not use Stack in stl..";
}
};
int main() {
#if defined(_GLIBCXX_STACK)
throw StackForbidden();
#endif
Stack<int> stack;
stack.push(88);
stack.push(44);
stack.push(99);
cout << "The size is: " << stack.size() << endl;
if (!stack.empty()) cout << stack.top() << endl;
print(stack);
stack.pop();
print(stack);
stack.push(777);
cout << "The size is: " << stack.size() << endl;
if (!stack.empty()) cout << stack.top() << endl;
print(stack);
stack.pop();
stack.pop();
cout << "The size is: " << stack.size() << endl;
print(stack);
stack.pop();
if (!stack.empty()) cout << stack.top() << endl;
else cout << "it is empty now." << endl;
Stack<double> stack1;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
stack1.push(i + 0.01);
}
print(stack1);
while (m--) {
stack1.pop();
}
cout << "The size is: " << stack1.size() << endl;
if (!stack1.empty()) cout << stack1.top() << endl;
print(stack1);
}
知识点1:
/*如果q1与q2都为空,那么往q1中插入元素
如果q1不为空,那么往q1中插入元素
如果q2不为空,那么往q1中插入元素
*/
知识点2:
/*在template<typename Y>和template<class Y>中,
typename和class的意义完全一样。*/
- template<typename Y>用两个queue实现stack
- 用两个stack实现queue
- 用两个stack实现Queue(java)
- 用两个栈(Stack)实现一个队列(Queue)
- 用两个栈(Stack)实现一个队列(Queue)
- 用两个栈(Stack)实现一个队列(Queue)
- 用两个队列(Queue)实现一个栈(Stack)
- Implement queue with two stack 用两个堆实现队列
- 用两个stack定义一个queue
- java操作两个stack实现一个queue(并发下)
- 使用两个栈(stack)实现一个队列(queue)
- C++ 用数组实现stack,queue
- stack实现queue ; list实现stack
- stack实现queue ; list实现stack
- 用模板template<typename T>实现int double等类型转string
- python实现stack 和 queue
- python实现Stack和Queue
- 模拟实现 stack和queue
- ZOJ--1037:Gridland
- 看雪CTF 2017 第六题设计思路和解题思路
- 【Bzoj3196】2B平衡树
- openGL纹理压缩概括
- java中的匿名内部类总结
- template<typename Y>用两个queue实现stack
- Spark分组二次排序
- 对象的创建和调用方法以及操作字段
- MySQL高级特性
- 线程安全与可重入函数
- 数据库——设计关系模式要用的概念
- [生产力工具]Travis CI浅入门
- Linux的多线程——生产者与消费者(条件变量、信号量)
- 安卓AR--DEMO以及卡片app下载地址