#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