数据结构之顺序栈(数组)C++(模板)

#ifndef STL_LINK_SEQUENCESTACK_H
#define STL_LINK_SEQUENCESTACK_H
/************************************************************************/
/*   以下是顺序栈(数组)C++ 简单不在注释
/************************************************************************/
enum Status
{
	EMPTY,
	HAVE,
	FULL
};

template <class Elemplent>
class SequenceStack
{
protected:
	int top;
	int maxSize;
	int countTotal;
	Elemplent *ptr;
public:
	SequenceStack(int size = 1000);
	SequenceStack(const SequenceStack<Elemplent>& tempPtr );
	SequenceStack<Elemplent> & operator= (const SequenceStack<Elemplent>& tempPtr);
	~SequenceStack();
	void InitSequenceStack(int size);
public:
	int GetLength();
	Status GetStatus();
	bool Push(const Elemplent& tempElemplent);
	bool GetTop(Elemplent & tempElemplent) const;
	bool Pop();
	bool Clear();
};

template<class Elemplent>
SequenceStack<Elemplent>::SequenceStack(int size )
{
	ptr = NULL;
	InitSequenceStack(size);
}
template<class Elemplent>
SequenceStack<Elemplent>::~SequenceStack()
{
	Clear();
	delete []ptr;
}
template <class Elemplent>
void SequenceStack<Elemplent>::InitSequenceStack(int size)
{
	maxSize =size;
	ptr = new Elemplent[maxSize]
	countTotal = 0;
}

template <class Elemplent>
int SequenceStack<Elemplent>::GetLength()
{
	return countTotal;
}

template <class Elemplent>
Status SequenceStack<Elemplent>::GetStatus()
{
	if (top == 0)
	{
		return EMPTY;
	}
	if (top ==maxSize)
	{
		return FULL;
	}
	return HAVE;
}

template <class Elemplent>
bool SequenceStack<Elemplent>::Push(const Elemplent& tempElemplent)
{
	if (FULL == GetStatus())
	{
		return false;
	}
	ptr[top++] = tempElemplent;
	countTotal++;
	return true;
}


template<class Elemplent>
bool SequenceStack<Elemplent>::GetTop(Elemplent & tempElemplent)const
{
	if (EMPTY == GetStatus())
	{
		return false;
	}
	tempElemplent = ptr[top];
	return true;

}

template<class Elemplent>
bool SequenceStack<Elemplent>::Pop()
{
	if (EMPTY == GetStatus())
	{
		return false;
	}
	top --;
	countTotal--;
	return true;
}

template<class Elemplent>
bool SequenceStack<Elemplent>::Clear()
{
	if (EMPTY == GetStatus())
	{
		return true;
	}
	top = 0;
	countTotal = 0;
	return true;
}

template<class Elemplent>
SequenceStack<Elemplent>::SequenceStack(const SequenceStack<Elemplent>& tempPtr )
{
	int length = tempPtr.countTotal;
	InitSequenceStack(tempPtr.maxSize);
	for (int i = 1;i<=length;i++)
	{
		ptr[i-1]  = tempPtr.ptr[i-1];
	}
	return *this;
}

template<class Elemplent>
SequenceStack<Elemplent>::operator=(const SequenceStack<Elemplent>& tempPtr )
{
	if (this = &tempPtr)
	{
		return *this;
	}
	int length = tempPtr.GetLength();
	Clear();
	InitSequenceStack(tempPtr.maxSize);
	for (int i = 1;i<= length;i++)
	{
		ptr[i-1] = tempPtr.ptr[i-1];
	}
	return *this;
}



#endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值