1002. The stack class
来源:互联网 发布:mac hosts文件位置 编辑:程序博客网 时间:2024/06/07 22:10
Description
You are to implement the following Stack class template, using the nontype parameter capacity to allocate the capacity of the stack, i.e. maximum elements that can be stored in the stack.
template<typename T, int capacity>
class Stack
{
public:
Stack(); // Constructs an empty stack.
bool empty(); // Returns true if the stack is empty.
T peek(); // Returns the element at the top of the stack without removing it from the stack.
void push(T value); // Stores an element into the top of the stack.
T pop(); // Removes the element at the top of the stack and returns it.
int size(); // Returns the number of elements in the stack.
private:
T* elements; // Points to an array that stores elements in the stack.
int num; // The number of the elements in the stack.
};
class Stack
{
public:
Stack(); // Constructs an empty stack.
bool empty(); // Returns true if the stack is empty.
T peek(); // Returns the element at the top of the stack without removing it from the stack.
void push(T value); // Stores an element into the top of the stack.
T pop(); // Removes the element at the top of the stack and returns it.
int size(); // Returns the number of elements in the stack.
private:
T* elements; // Points to an array that stores elements in the stack.
int num; // The number of the elements in the stack.
};
Your submitted source code should only include the implementation of Stack class template, without the declaration above.
No main() function should be included.
// Problem#: 14402// Submission#: 3726490// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<iostream>using namespace std;template<typename T, int capacity>Stack<T,capacity> :: Stack(){ elements=new T[capacity]; num=0; } template<typename T, int capacity>bool Stack<T,capacity> :: empty(){ return num==0; }template<typename T, int capacity>T Stack<T,capacity> :: peek(){ return elements[num-1]; }template<typename T, int capacity>void Stack<T,capacity> :: push(T value){ elements[num++]=value;}template<typename T, int capacity>T Stack<T,capacity> :: pop(){ return elements[--num]; }template<typename T, int capacity>int Stack<T,capacity> :: size(){ return num;}
0 0
- 1002. The stack class
- Stack Class
- 9. 13. 7. Demonstrate the generic Stack class.
- Class not found using the boot class loader; no stack available
- Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack ava
- Android studio异常Class not found using the boot class loader; no stack available
- android studio 错误:Class not found using the boot class loader; no stack available
- the Stack
- android启动应用java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack tra
- Generics 通用型 a stack class
- Playing with the Stack
- Playing with the Stack
- Playing with the stack
- The Stack Data Structure
- The Full Stack
- The Linux Graphics Stack
- The using of stack
- The operate of stack
- HTML5之智能表单
- Java--截取路径字符串
- 高效率的YUV转RGB的OnPaint操作
- 两个栈实现一个队列
- HDU-4445 Crazy Tank
- 1002. The stack class
- Windows笔记本上开启Wi-Fi
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"问题的解决办法
- 奇数个的那个数(异或问题)
- 超全的JAVA常见名词解释
- C语言下的二进制文件读写
- html布局
- 在Ubuntu下安装搜狗输入法和搜狗输入法的相关设置
- Android 常用方法使用注意