自己动手写数据结构:Stack类模板的动态数组实现

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值