2742 Review Template and Stack (eden)
来源:互联网 发布:一流网络空间安全学院 编辑:程序博客网 时间:2024/06/05 03:56
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.
出题人:黎洋
Provided Codes
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);}
Submission
Stack.h
参照
#ifndef STACK_H#define STACK_H#include <queue>#include <iostream>using namespace std;template <typename Type> void print(Type); template <typename T>class Stack { public: Stack(); // constructor. void push(const T& data); // push operation. T pop(); // return the value in the top and pop it out of the stack. T top(); // return the value in top. int size() const; // return size of the stack. bool empty(); // check whether is empty. friend void print<>(Stack<T> a); private: queue<T> q1; // two queues. queue<T> q2; int count; // the number of elements.};template <typename T>Stack<T>::Stack(){ count=0;}template <typename T>void Stack<T>::push(const T& data){ q1.push(data); ++count;}template <typename T>T Stack<T>::pop(){ if(empty()) return -1; T tem=q1.back(); --count; for(int i=0;i<count;i++){ q2.push(q1.front()); q1.pop(); } q1.pop(); for(int i=0;i<count;i++){ q1.push(q2.front()); q2.pop(); } return tem;}template <typename T>T Stack<T>::top(){ return q1.back();}template <typename T>int Stack<T>::size() const{ return count;}template <typename T>bool Stack<T>::empty(){ if(count==0) return true; return false;}template <typename Type>void print(Type a){ int tem=a.size(); for(int i=0;i<tem;i++) cout<<a.pop()<<" "; cout<<endl;}#endif
阅读全文
0 0
- 2742 Review Template and Stack (eden)
- 2739 Stack with Template (eden)
- 2740 Template Compare (eden)
- 2738 Queue with Template(eden)
- [STL]IP and QQ (eden)
- Stack-c++template
- Stack-c++ template
- Tools - Template Sequence stack
- [C++]Stack with Template
- Stack with Template
- 【TCL and Expect 】 review
- Tuxedo and PeopleSoft review
- permulation and review
- Stack Using a C++ Template:
- Call stack and stack frame
- C++ Template and Inheritance
- template and getchar()
- Algorithm and Data Structure Review
- Win7怎么查看系统日志?
- Could not publish to the server. java.lang.NullPointerException
- 【Linux】centOS配置JavaWeb环境--1-->jdk及MySQL配置
- 【greedy】LeetCode----455. Assign Cookies
- git从本地到远程常用命令总结
- 2742 Review Template and Stack (eden)
- unity简单自由漫游摄像机控制脚本
- JS的预解析和作用域浅析
- Android中的属性,是怎么应用到控件上的(二)
- BZOJ 1066: [SCOI2007]蜥蜴 最大流
- 堆栈与队列
- C语言学习笔记——指针
- C#动态链接库的创建及使用
- Matlab批量读取文件夹文件