Stack类模板的动态数组实现(基本功能)
2012-08-02 08:56
393 查看
#ifndef MYSTACK_H #define MYSTACK_H #include <iostream> template <class T> class MyStack { public: MyStack():val(new T[_CAP]), _size(0), _cap(_CAP) {} MyStack(const MyStack& s); ~MyStack(); void _pop(); void _push(const T& x); bool _empty() const; int getSize() const; void _clear(); void _popBottom(); T& _top(); T& _bottom(); MyStack& operator = (const MyStack& s); bool operator += (const MyStack& s); bool operator == (const MyStack& s); protected: private: T* val; int _size; int _cap; static const int _CAP = 4; void rebuild(const int cap); }; template <class T> MyStack<T>::MyStack(const MyStack& s) : val(new T[s._cap]), _size(s._size), _cap(s._cap) { for (int i = 0; i < _cap; i ++) { val[i] = s.val[i]; } } template <class T> MyStack<T>::~MyStack() { if (!_empty()) { delete [] val; } } template <class T> MyStack<T>& MyStack<T>::operator = (const MyStack& s) { if (!val) { val = new T[s._cap]; } _size = s._size; _cap = s._cap; for (int i = 0; i < _cap; i ++) { val[i] = s.val[i]; } } template <class T> bool MyStack<T>::operator == (const MyStack& s) { bool flag = false; if (s._size == _size && s._cap == s._cap) { flag = true; MyStack<T> temp(s); MyStack<T> temp2(*this); while (!temp._empty()) { if(temp._top() != temp2._top()) flag = false; temp._pop(); temp2._pop(); } } return flag; } template <class T> bool MyStack<T>::operator += (const MyStack& s) { if (s.getSize() > getSize()) { rebuild(s.getSize()); } MyStack<T> temp(*this); MyStack<T> src(s); _clear(); while (!temp._empty()) { temp._top() += src._top(); _push(temp._top()); temp._pop(); src._pop(); while (src._empty() && !temp._empty()) { _push(temp._top()); temp._pop(); } } reverseStack(*this); return 0; } //若栈容量不够则重新分配 template <class T> void MyStack<T>::rebuild(const int cap) { //int _cap = 2*_CAP; T* temp = new T[cap]; for (int i = 0; i < _size; i ++) { temp[i] = val[i]; } delete [] val; val = temp; } template <class T> T& MyStack<T>::_top() { return val[_size-1]; }
template <class T> void MyStack<T>::_pop() { --_size; }
template <class T> void MyStack<T>::_push(const T& x) { if (_size >= _CAP) { rebuild(2*_CAP); } val[_size++] = x; } template <class T> int MyStack<T>::getSize() const { return _size; } template <class T> bool MyStack<T>::_empty() const { return _size == 0; } template <class T> void MyStack<T>::_clear() { while (!_empty()) { _pop(); } } //反转栈内元素 template <class T> void reverseStack(MyStack<T>& s) { MyStack<T> temp(s); while(s.getSize() > 0) { s._pop(); } while(temp.getSize() > 0) { s._push(temp._top()); temp._pop(); } }
//弹出栈底元素 template <class T> void MyStack<T>::_popBottom() { reverseStack(*this); _pop(); reverseStack(*this); }
//返回栈底元素 template <class T> T& MyStack<T>::_bottom() { //reverseStack(*this); //T& b = _top(); //reverseStack(*this); //注意:仍然返回的是栈顶元素! MyStack<T> temp; while(!_empty()) { temp._push(_top()); _pop(); } //T& b = temp._top(); //error!不能返回局部对象 _push(temp._top()); temp._pop(); T& b = _top(); while(!temp._empty()) { _push(temp._top()); temp._pop(); } return b; } #endif
相关文章推荐
- 通过动态内存分配来实现类似于动态数组的功能
- 数据结构C语言版--动态顺序表的基本功能实现(二)
- JavaScript数组,JSON对象实现动态添加、修改、删除功能示例
- avalon实现数组动态创建功能
- c#的动态按钮数组的添加与功能实现
- 动态代理手动实现Spring AOP的基本功能
- C实现数组的基本功能
- 动态可变字符数组的实现, 以及用于实现读取文件中的完整一行字符的功能
- 通过数组实现类似于集合的功能(包含动态扩容、前平移、后平移、增删改查)
- Java - - 数组实现栈基本功能
- 用两个栈实现一个队列的基本功能
- 面试5之编写一个方法,实现基本的字符串压缩功能。
- 单链表的基本功能 与扩展功能的实现
- 使用zTree实现导航树、angular实现数据动态绑定功能
- 使用数组实现栈stack功能
- mongoose创建数据结构,实现基本增删改查功能
- Android 8.1 Launcher3实现动态指针时钟功能
- 用简单的css实现html网页动态电子相册的功能
- 数组实现动态栈,java
- 动态注册HttpModule管道,实现global.asax功能