两个队列实现栈功能
2016-05-21 11:21
253 查看
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:
1. You are not allowed to use any STL except "queue".
2. For more detail, see the codes in main.cpp.
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:
1. You are not allowed to use any STL except "queue".
2. For more detail, see the codes in main.cpp.
#include <iostream> #include <queue> using namespace std; template <typename T> class Stack { // 两个队列相互操作减少复杂度 private: queue<T> p1; queue<T> p2; int count; public: Stack() { count = 0; } void push(const T data) { if (p2.size() > 0) { p2.push(data); } else { p1.push(data); } ++count; } T pop() { T temp; if (!empty()) { if (p1.empty()) { while (p2.size() > 1) { p1.push(p2.front()); p2.pop(); } temp = p2.front(); p2.pop(); } else { while (p1.size() > 1) { p2.push(p1.front()); p1.pop(); } temp = p1.front(); p1.pop(); } --count; } return temp; } T top() { if (!p1.empty()) return p1.back(); return p2.back(); } T size() const { return count; } bool empty() { return count == 0; } }; template <typename T> void print(Stack <T> t) { while (!t.empty()) { cout << t.top() << " "; t.pop(); } cout << endl; }main函数:
#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); }
相关文章推荐
- android 常用的几个框架
- 关于tomcat 部署项目的思考
- 迅雷数据运营踩过的坑视频笔记
- 统计分析Android文档
- 贝塞尔校正系数
- ABAQUS 免费批量提交器
- jQuery增加千分符
- 第十三周项目-阅读程序-3
- 微博营销6——有效的产品销售模式
- OJ第三题
- jekyll博客搭建及其可能出现的问题(1)
- 安卓环境配置
- 有关C#标签Attribute的熟悉
- 数据库操作封装类 DBHelper.cs
- jni调用java数组导致VM aborting,安卓程序莫名闪退
- Iwfu-ListView(三)-万能适配器
- 日期与时间函数与夏令时的关系
- 关于:[Struts2]Caused by: com.opensymphony.xwork2.inject.DependencyException:
- mtk平台如何屏蔽modem,蓝牙以及sd卡
- AIX 移植PV