【c++习题】【17/4/13】stack
2017-05-08 18:32
579 查看
1、stack 模板、动态内存分配、析构
1 #include "stack2.cpp" #include <iostream> using namespace std; int main() { // 测试int型 Stack<int> s1(5); s1.push(1); s1.push(2); s1.push(3); s1.push(4); s1.push(5); if(!s1.isEmpty()) cout << s1.pop() << endl; cout << s1.pop() << endl; cout << s1.pop() << endl; cout << s1.pop() << endl; cout << s1.pop() << endl; cout << endl; // 测试char型 Stack<char> s2(6); if(!s2.isFull()) s2.push('a'); s2.push('b'); s2.push('c'); s2.push('d'); s2.push('e'); s2.push('f'); cout << s2.pop() << endl; cout << s2.pop() << endl; cout << s2.pop() << endl; cout << s2.pop() << endl; cout << s2.pop() << endl; //cout << s2.pop() << endl; return 0; } 2 template <class T> class Stack { private: T *sptr; int size; int Top; public: // 构造器 Stack() { sptr = new T[10]; size = 10; Top = 0; } Stack(int n) { sptr = new T ; size = n; Top = 0; } // 析构函数 ~Stack() { delete []sptr; } // push void push(T i) { *(sptr++) = i; ++Top; } // pop T pop() { return *(--sptr); --Top; } bool isEmpty() { if (Top == 0) return true; return false; } bool isFull() { if (Top < size) return false; return true; } };
相关文章推荐
- C++并发实战17:线程安全的stack和queue
- 【c++习题】【17/5/8】重载运算符
- 谭浩强C++程序设计_指针习题13
- 算法竞赛入门经典(第二版)-刘汝佳-第五章 C++与STL 习题(13/16)
- 笨办法学Python-习题13-17
- 【c++习题】【17/4/16】动态分配内存
- 算法导论第十三章习题13-3——AVL树(高度平衡树)C++代码详细实现
- 【c++习题】【17/5/22】重载数组下标操作符
- C++ 修改的p369-17题 数组类
- ANSI/ISO C++ Professional Programmer's Handbook 13
- 钱能C++习题9.3
- C++学习笔记——第二章习题(三)
- C++复习 13 复制控制
- C++习题1
- C++学习笔记——第二章习题(四)-用循环语句打印图案
- |1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20所有这些字符串,把它的数字一个个剥离??
- C和C++实现Stack
- C++学习笔记-第二章习题(二)
- Exceptional C++ Style之13
- More Effective C++之13